diff options
| author | Satoshi Tanaka <[email protected]> | 2023-05-01 06:54:26 +0900 |
|---|---|---|
| committer | Satoshi Tanaka <[email protected]> | 2023-05-01 06:54:26 +0900 |
| commit | a186694fddd00beb4c3b45349cd79ca1959b4d17 (patch) | |
| tree | 015ea8bd568d6d8a3b86957b3d10f32bd3fe1989 | |
| parent | 099ec7443bed2183397005b4e8ebfcd2492e2b4c (diff) | |
Implement WPA2 AP mode
| -rw-r--r-- | src/consts.rs | 15 | ||||
| -rw-r--r-- | src/control.rs | 32 |
2 files changed, 46 insertions, 1 deletions
diff --git a/src/consts.rs b/src/consts.rs index ade3cb2c5..1f6551589 100644 --- a/src/consts.rs +++ b/src/consts.rs | |||
| @@ -112,6 +112,21 @@ pub(crate) const READ: bool = false; | |||
| 112 | pub(crate) const INC_ADDR: bool = true; | 112 | pub(crate) const INC_ADDR: bool = true; |
| 113 | pub(crate) const FIXED_ADDR: bool = false; | 113 | pub(crate) const FIXED_ADDR: bool = false; |
| 114 | 114 | ||
| 115 | pub(crate) const AES_ENABLED: u32 = 0x0004; | ||
| 116 | pub(crate) const WPA2_SECURITY: u32 = 0x00400000; | ||
| 117 | |||
| 118 | pub(crate) const MIN_PSK_LEN: usize = 8; | ||
| 119 | pub(crate) const MAX_PSK_LEN: usize = 64; | ||
| 120 | |||
| 121 | // Security type (authentication and encryption types are combined using bit mask) | ||
| 122 | #[allow(non_camel_case_types)] | ||
| 123 | #[derive(Copy, Clone, PartialEq)] | ||
| 124 | #[repr(u32)] | ||
| 125 | pub(crate) enum Security { | ||
| 126 | OPEN = 0, | ||
| 127 | WPA2_AES_PSK = WPA2_SECURITY | AES_ENABLED, | ||
| 128 | } | ||
| 129 | |||
| 115 | #[allow(non_camel_case_types)] | 130 | #[allow(non_camel_case_types)] |
| 116 | #[derive(Copy, Clone)] | 131 | #[derive(Copy, Clone)] |
| 117 | #[repr(u8)] | 132 | #[repr(u8)] |
diff --git a/src/control.rs b/src/control.rs index 440246498..e1ad06e6b 100644 --- a/src/control.rs +++ b/src/control.rs | |||
| @@ -227,6 +227,20 @@ impl<'a> Control<'a> { | |||
| 227 | } | 227 | } |
| 228 | 228 | ||
| 229 | pub async fn start_ap_open(&mut self, ssid: &str, channel: u8) { | 229 | pub async fn start_ap_open(&mut self, ssid: &str, channel: u8) { |
| 230 | self.start_ap(ssid, "", Security::OPEN, channel).await; | ||
| 231 | } | ||
| 232 | |||
| 233 | pub async fn start_ap_wpa2(&mut self, ssid: &str, passphrase: &str, channel: u8) { | ||
| 234 | self.start_ap(ssid, passphrase, Security::WPA2_AES_PSK, channel).await; | ||
| 235 | } | ||
| 236 | |||
| 237 | async fn start_ap(&mut self, ssid: &str, passphrase: &str, security: Security, channel: u8) { | ||
| 238 | if security != Security::OPEN | ||
| 239 | && (passphrase.as_bytes().len() < MIN_PSK_LEN || passphrase.as_bytes().len() > MAX_PSK_LEN) | ||
| 240 | { | ||
| 241 | panic!("Passphrase is too short or too long"); | ||
| 242 | } | ||
| 243 | |||
| 230 | // Temporarily set wifi down | 244 | // Temporarily set wifi down |
| 231 | self.ioctl(IoctlType::Set, IOCTL_CMD_DOWN, 0, &mut []).await; | 245 | self.ioctl(IoctlType::Set, IOCTL_CMD_DOWN, 0, &mut []).await; |
| 232 | 246 | ||
| @@ -254,7 +268,23 @@ impl<'a> Control<'a> { | |||
| 254 | self.ioctl_set_u32(IOCTL_CMD_SET_CHANNEL, 0, channel as u32).await; | 268 | self.ioctl_set_u32(IOCTL_CMD_SET_CHANNEL, 0, channel as u32).await; |
| 255 | 269 | ||
| 256 | // Set security | 270 | // Set security |
| 257 | self.set_iovar_u32x2("bsscfg:wsec", 0, 0).await; // wsec = open | 271 | self.set_iovar_u32x2("bsscfg:wsec", 0, (security as u32) & 0xFF).await; |
| 272 | |||
| 273 | if security != Security::OPEN { | ||
| 274 | self.set_iovar_u32x2("bsscfg:wpa_auth", 0, 0x0084).await; // wpa_auth = WPA2_AUTH_PSK | WPA_AUTH_PSK | ||
| 275 | |||
| 276 | Timer::after(Duration::from_millis(100)).await; | ||
| 277 | |||
| 278 | // Set passphrase | ||
| 279 | let mut pfi = PassphraseInfo { | ||
| 280 | len: passphrase.as_bytes().len() as _, | ||
| 281 | flags: 1, // WSEC_PASSPHRASE | ||
| 282 | passphrase: [0; 64], | ||
| 283 | }; | ||
| 284 | pfi.passphrase[..passphrase.as_bytes().len()].copy_from_slice(passphrase.as_bytes()); | ||
| 285 | self.ioctl(IoctlType::Set, IOCTL_CMD_SET_PASSPHRASE, 0, &mut pfi.to_bytes()) | ||
| 286 | .await; | ||
| 287 | } | ||
| 258 | 288 | ||
| 259 | // Change mutlicast rate from 1 Mbps to 11 Mbps | 289 | // Change mutlicast rate from 1 Mbps to 11 Mbps |
| 260 | self.set_iovar_u32("2g_mrate", 11000000 / 500000).await; | 290 | self.set_iovar_u32("2g_mrate", 11000000 / 500000).await; |
