1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Configurable and common handlers that act on PHYs in response to events.
//!
//! This module provides recipes for event handlers in hardware simulator tests. Handlers are
//! constructed using functions and always emit [`ActionResult`]s as output. These recipes can be
//! composed to form complex event handlers using terse and declarative syntax.
//!
//! # Examples
//!
//! Actions are exposed as functions that construct event handlers. The following example
//! demonstrates constructing a client event handler that scans and transmits packets to an AP.
//!
//! ```rust,ignore
//! let advertisements = [ProbeResponse { /* ... */ }];
//! let mut handler = branch::or((
//!     event::on_scan(action::send_advertisements_and_scan_completion(&client, advertisements)),
//!     event::on_transmit(action::send_packet(&ap, rx_info_with_default_ap())),
//! ));
//! ```
//!
//! A more complex action handles client association with an authentication tap for controlling the
//! authentication process. The following example constructs such a handler.
//!
//! ```rust,ignore
//! let beacons = [Beacon { /* ... */ }];
//! let control = AuthenticationControl {
//!     updates: UpdateSink::new(),
//!     authenticator: /* ... */,
//! };
//! let tap = AuthenticationTap {
//!     control: &mut control,
//!     // This event handler reacts to `AuthenticationEvent`s and is passed the
//!     // `AuthenticationControl` as state.
//!     handler: action::authenticate_with_control_state(),
//! };
//! let mut handler = branch::or((
//!     event::on_scan(action::send_advertisements_and_scan_completion(&client, beacons)),
//!     event::on_transmit(action::connect_with_authentication_tap(
//!         &client, &ssid, &bssid, &channel, &protection, tap,
//!     )),
//! ));
//! ```

use {
    anyhow::{bail, Context},
    fidl_fuchsia_wlan_ieee80211 as fidl_ieee80211, fidl_fuchsia_wlan_mlme as fidl_mlme,
    fidl_fuchsia_wlan_tap as fidl_tap,
    ieee80211::{Bssid, MacAddrBytes, Ssid},
    tracing::{debug, info},
    wlan_common::{bss::Protection, channel::Channel, mac},
    wlan_rsn::{auth, rsna::UpdateSink, Authenticator},
};

use crate::{
    event::{
        self, branch,
        buffered::{AssocReqFrame, AuthFrame, Buffered, DataFrame, ProbeReqFrame},
        Handler, Stateful,
    },
    ApAdvertisement, ProbeResponse, CLIENT_MAC_ADDR,
};

/// The result of an action event handler.
///
/// All event handlers provided in this module emit this type as their output.
pub type ActionResult = Result<(), anyhow::Error>;

/// Authentication state and handler used to tap into the client authentication process in an event
/// handler.
///
/// See [`connect_with_authentication_tap`].
///
/// [`connect_with_authentication_tap`]: crate::event::action::connect_with_authentication_tap
#[derive(Debug)]
pub struct AuthenticationTap<'a, H> {
    pub control: &'a mut AuthenticationControl,
    /// An event handler that authenticates a client with an AP.
    ///
    /// The event handler must accept [`AuthenticationControl`] state and [`AuthenticationEvent`]
    /// events.
    ///
    /// [`AuthenticationControl`]: crate::event::action::AuthenticationControl
    /// [`AuthenticationEvent`]: crate::event::action::AuthenticationEvent
    pub handler: H,
}

impl<'a, H> AuthenticationTap<'a, H> {
    fn call<'e>(&mut self, event: &AuthenticationEvent<'e>) -> ActionResult
    where
        H: Handler<AuthenticationControl, AuthenticationEvent<'e>, Output = ActionResult>,
    {
        self.handler
            .by_ref()
            .context("failed to handle authentication event")
            .call(self.control, event)
            .matched()
            .unwrap_or(Ok(()))
    }
}

/// An authentication event that occurs as part of a [`TxArgs`].
///
/// See [`AuthenticationTap`].
///
/// [`AuthenticationTap`]: crate::event::action::AuthenticationTap
/// [`TxArgs`]: fidl_fuchsia_wlan_tap::TxArgs
#[derive(Debug)]
pub struct AuthenticationEvent<'a> {
    pub phy: &'a fidl_tap::WlantapPhyProxy,
    pub bssid: &'a Bssid,
    pub channel: &'a Channel,
    pub is_ready_for_sae: bool,
    pub is_ready_for_eapol: bool,
}

/// Authenticator (protocol, credentials, etc.) and message buffer used for client authentication.
#[derive(Debug)]
pub struct AuthenticationControl {
    pub authenticator: Authenticator,
    pub updates: UpdateSink,
}

pub fn send_advertisements<'h, S, E, I>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    advertisements: I,
) -> impl Handler<S, E, Output = ActionResult> + 'h
where
    S: 'h,
    E: 'h,
    I: IntoIterator,
    I::Item: ApAdvertisement + 'h,
{
    let advertisements: Vec<_> = advertisements.into_iter().collect();
    event::matched(move |_: &mut S, _: &E| {
        for advertisement in advertisements.iter() {
            advertisement.send(phy).context("failed to send AP advertisement")?;
        }
        Ok(())
    })
}

pub fn send_scan_completion<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    status: i32,
) -> impl Handler<S, fidl_tap::StartScanArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    use fuchsia_zircon::Duration;

    const SCAN_COMPLETION_DELAY: Duration = Duration::from_seconds(2i64);

    event::matched(move |_: &mut S, event: &fidl_tap::StartScanArgs| {
        tracing::info!(
            "TODO(https://fxbug.dev/42060050): Sleeping for {} second(s) before sending scan completion.",
            SCAN_COMPLETION_DELAY.into_seconds()
        );
        SCAN_COMPLETION_DELAY.sleep();
        phy.scan_complete(event.scan_id, status).context("failed to send scan completion")
    })
}

pub fn send_advertisements_and_scan_completion<'h, S, I>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    advertisements: I,
) -> impl Handler<S, fidl_tap::StartScanArgs, Output = ActionResult> + 'h
where
    S: 'h,
    I: IntoIterator,
    I::Item: ApAdvertisement + 'h,
{
    event::matched(|_, event: &fidl_tap::StartScanArgs| {
        debug!(
            "Sending AP advertisements and scan completion for scan event with ID: {:?}",
            event.scan_id,
        );
    })
    .and(send_advertisements(phy, advertisements))
    .try_and(send_scan_completion(phy, 0))
}

pub fn send_probe_response<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    ssid: &'h Ssid,
    bssid: &'h Bssid,
    channel: &'h Channel,
    protection: &'h Protection,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    event::extract(|_: Buffered<ProbeReqFrame>| {
        // NOTE: Normally the AP only sends probe responses on its channel, but hardware simulator
        //       does not support this at time of writing. This does not (yet) affect tests.
        ProbeResponse {
            channel: *channel,
            bssid: *bssid,
            ssid: ssid.clone(),
            protection: *protection,
            rssi_dbm: -10,
            wsc_ie: None,
        }
        .send(phy)
        .context("failed to send probe response frame")
    })
}

pub fn send_packet<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    rx_info: fidl_tap::WlanRxInfo,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    event::matched(move |_: &mut S, event: &fidl_tap::TxArgs| {
        phy.rx(&event.packet.data, &rx_info).context("failed to send packet")
    })
}

pub fn send_open_authentication<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    bssid: &'h Bssid,
    channel: &'h Channel,
    status: impl Into<mac::StatusCode>,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    let status = status.into();
    event::extract(move |_: Buffered<AuthFrame>| {
        crate::send_open_authentication(channel, bssid, status, phy)
            .context("failed to send authentication frame")
    })
}

// TODO(https://fxbug.dev/42080468): Preserve the updates in the sink for logging, inspection, etc.
pub fn authenticate_with_control_state<'h>(
) -> impl Handler<AuthenticationControl, AuthenticationEvent<'h>, Output = ActionResult> + 'h {
    event::matched(|control: &mut AuthenticationControl, event: &AuthenticationEvent<'_>| {
        use wlan_rsn::rsna::SecAssocUpdate::{TxEapolKeyFrame, TxSaeFrame};

        let mut index = 0;
        while index < control.updates.len() {
            match &control.updates[index] {
                TxSaeFrame(ref frame) => {
                    if event.is_ready_for_sae {
                        crate::send_sae_authentication_frame(
                            &frame,
                            &event.channel,
                            &event.bssid,
                            event.phy,
                        )
                        .context("failed to send SAE authentication frame")?;
                        control.updates.remove(index);
                        continue; // The update has been removed: do NOT increment the index.
                    } else {
                        debug!("authentication: received unexpected SAE frame");
                    }
                }
                TxEapolKeyFrame { ref frame, .. } => {
                    if event.is_ready_for_eapol {
                        crate::rx_wlan_data_frame(
                            &event.channel,
                            &CLIENT_MAC_ADDR,
                            &event.bssid.clone().into(),
                            &event.bssid.clone().into(),
                            &frame[..],
                            mac::ETHER_TYPE_EAPOL,
                            event.phy,
                        )?;
                        control.updates.remove(index);
                        control
                            .authenticator
                            .on_eapol_conf(
                                &mut control.updates,
                                fidl_mlme::EapolResultCode::Success,
                            )
                            .context("failed to send EAPOL confirm")?;
                        continue; // The update has been removed: do NOT increment the index.
                    } else {
                        debug!("authentication: received unexpected EAPOL key frame");
                    }
                }
                _ => {}
            }
            index += 1;
        }
        Ok(())
    })
}

pub fn send_association_response<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    bssid: &'h Bssid,
    channel: &'h Channel,
    status: impl Into<mac::StatusCode>,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    let status = status.into();
    event::extract(move |_: Buffered<AssocReqFrame>| {
        crate::send_association_response(channel, bssid, status, phy)
            .context("failed to send association response frame")
    })
}

pub fn connect_with_open_authentication<'h, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    ssid: &'h Ssid,
    bssid: &'h Bssid,
    channel: &'h Channel,
    protection: &'h Protection,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    S: 'h,
{
    branch::or((
        send_open_authentication(phy, bssid, channel, fidl_ieee80211::StatusCode::Success),
        send_association_response(phy, bssid, channel, fidl_ieee80211::StatusCode::Success),
        send_probe_response(phy, ssid, bssid, channel, protection),
    ))
}

pub fn connect_with_authentication_tap<'h, H, S>(
    phy: &'h fidl_tap::WlantapPhyProxy,
    ssid: &'h Ssid,
    bssid: &'h Bssid,
    channel: &'h Channel,
    protection: &'h Protection,
    tap: AuthenticationTap<'h, H>,
) -> impl Handler<S, fidl_tap::TxArgs, Output = ActionResult> + 'h
where
    H: Handler<AuthenticationControl, AuthenticationEvent<'h>, Output = ActionResult> + 'h,
    S: 'h,
{
    type Tap<'a, H> = AuthenticationTap<'a, H>;

    let authenticate =
        event::extract(Stateful(|tap: &mut Tap<'_, H>, frame: Buffered<AuthFrame>| {
            let frame = frame.get();
            match frame.auth_hdr.auth_alg_num {
                mac::AuthAlgorithmNumber::OPEN => {
                    if matches!(
                        tap.control.authenticator,
                        Authenticator { auth_cfg: auth::Config::ComputedPsk(_), .. },
                    ) {
                        crate::send_open_authentication(
                            channel,
                            bssid,
                            fidl_ieee80211::StatusCode::Success,
                            phy,
                        )
                        .context("failed to send open authentication frame")?;
                        tap.call(&AuthenticationEvent {
                            phy,
                            bssid,
                            channel,
                            is_ready_for_sae: false,
                            is_ready_for_eapol: false,
                        })
                    } else {
                        bail!("open authentication frame is incompatible with authenticator");
                    }
                }
                mac::AuthAlgorithmNumber::SAE => {
                    info!("auth_txn_seq_num: {}", { frame.auth_hdr.auth_txn_seq_num });
                    // Reset the authenticator and clear the update sink so that the PMK is not
                    // observed from the first connection attempt. The SAE handshake uses multiple
                    // frames so this reset only occurs on the first authentication frame.
                    // NOTE: A different approach is needed to test the retransmission of the first
                    //       authentication frame.
                    if frame.auth_hdr.auth_txn_seq_num == 1 {
                        tap.control.authenticator.reset();
                        tap.control.updates.clear();
                    }

                    tap.control
                        .authenticator
                        .on_sae_frame_rx(
                            &mut tap.control.updates,
                            fidl_mlme::SaeFrame {
                                peer_sta_address: bssid.to_array(),
                                status_code: frame
                                    .auth_hdr
                                    .status_code
                                    .into_fidl_or_refused_unspecified(),
                                seq_num: frame.auth_hdr.auth_txn_seq_num,
                                sae_fields: frame.elements.to_vec(),
                            },
                        )
                        .context("failed to process SAE frame with authenticator")?;
                    tap.call(&AuthenticationEvent {
                        phy,
                        bssid,
                        channel,
                        is_ready_for_sae: true,
                        is_ready_for_eapol: false,
                    })
                }
                auth_alg_num => {
                    bail!("unexpected authentication algorithm: {:?}", auth_alg_num);
                }
            }
        }));
    let associate = event::extract(Stateful(|tap: &mut Tap<'_, H>, _: Buffered<AssocReqFrame>| {
        crate::send_association_response(channel, bssid, fidl_ieee80211::StatusCode::Success, phy)
            .context("failed to send association response frame")?;
        match tap.control.authenticator.auth_cfg {
            auth::Config::ComputedPsk(_) => tap.control.authenticator.reset(),
            auth::Config::DriverSae { .. } => {
                bail!("hardware simulator does not support driver SAE");
            }
            auth::Config::Sae { .. } => {}
        }
        tap.control
            .authenticator
            .initiate(&mut tap.control.updates)
            .context("failed to initiate authenticator")?;
        tap.call(&AuthenticationEvent {
            phy,
            bssid,
            channel,
            is_ready_for_sae: true,
            is_ready_for_eapol: true,
        })
    }));
    let eapol = event::extract(Stateful(|tap: &mut Tap<'_, H>, frame: Buffered<DataFrame>| {
        // EAPOL frames are transmitted as LLC data frames.
        for mac::Msdu { llc_frame, .. } in frame.get() {
            assert_eq!(llc_frame.hdr.protocol_id.to_native(), mac::ETHER_TYPE_EAPOL);
            let mic_size = tap.control.authenticator.get_negotiated_protection().mic_size;
            let key_frame_rx = eapol::KeyFrameRx::parse(mic_size as usize, llc_frame.body)
                .context("failed to parse EAPOL frame")?;
            tap.control
                .authenticator
                .on_eapol_frame(&mut tap.control.updates, eapol::Frame::Key(key_frame_rx))
                .context("failed to process EAPOL frame with authenticator")?;
            tap.call(&AuthenticationEvent {
                phy,
                bssid,
                channel,
                is_ready_for_sae: true,
                is_ready_for_eapol: true,
            })?;
        }
        Ok(())
    }));

    event::with_state(
        tap,
        branch::or((
            authenticate,
            associate,
            send_probe_response(phy, ssid, bssid, channel, protection),
            eapol,
        )),
    )
}