wifi_lab
Clone
https://git.tahinli.com/Tahinli/wifi_lab.git
0 watching0 forksRust2.1 MiBb265998Tahinli1 month ago27 commits
| 1 | //! System interface control via `ip`/`iw` subprocess (boring + portable). |
| 2 | //! |
| 3 | //! The interface must not be under active control of NetworkManager or |
| 4 | //! wpa_supplicant while in monitor mode — that interference is the caller's |
| 5 | //! concern (this module intentionally does NOT kill daemons like airmon-ng). |
| 6 | |
| 7 | use Command; |
| 8 | use Mutex; |
| 9 | use ; |
| 10 | |
| 11 | use ; |
| 12 | |
| 13 | use crateMacAddr; |
| 14 | |
| 15 | /// Run a command, capturing stderr into the anyhow context on failure. |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | /// RF-channel arbitration shared by the hopping scanner and the attack |
| 40 | /// workers. The scanner consults [`ChannelPin::get`] once per hop cycle: |
| 41 | /// nonzero → dwell on that frequency; zero → normal hop. Attack workers |
| 42 | /// move the pin instead of fighting the hopper with their own `set_freq` |
| 43 | /// calls (the scanner re-asserts the pinned value, not the hop plan). |
| 44 | /// |
| 45 | /// Two layers: `holds` is the stack of long-lived holders' desired MHz |
| 46 | /// (the handshake-capture workers — the radio must get the channel BACK |
| 47 | /// after a short interruption), `active` is what the scanner currently |
| 48 | /// must dwell on. A deauth worker briefly |
| 49 | /// [`override_freq`](ChannelPin::override_freq)s `active` per band, then |
| 50 | /// [`settle`](ChannelPin::settle)s to restore the top holder's frequency |
| 51 | /// (or free hop when nothing is held). |
| 52 | /// |
| 53 | /// Holds are REFCOUNTED, not single-owner: two concurrent captures on the |
| 54 | /// same band each [`hold`](ChannelPin::hold) the same MHz, and the pin |
| 55 | /// frees only when the LAST [`release`](ChannelPin::release) drops — the |
| 56 | /// first capture's worker exiting early must not un-pin the channel out |
| 57 | /// from under the second. The TOP of the stack (most recent hold of a |
| 58 | /// still-held frequency) is the owner whose frequency `settle` restores. |
| 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 | /// True when `ifname` shows up as a netdev in `iw dev` output. |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | /// Enter monitor mode by creating a DEDICATED monitor vif `<ifname>mon` |
| 142 | /// and bringing the original STA vif DOWN. |
| 143 | /// |
| 144 | /// Why never switch type in place: on iwlwifi (verified on this lab box, |
| 145 | /// AX-class) a STA vif that has ever been associated drags a firmware |
| 146 | /// channel-context with it into monitor type — `iw set channel`/`set freq` |
| 147 | /// succeed at the cfg80211 level, `iw dev info` shows the new channel, yet |
| 148 | /// the RX path demonstrably stays pinned to the old association channel |
| 149 | /// (only that AP's frames ever arrive). A freshly created monitor vif has |
| 150 | /// no such context and receives everything, with the STA vif merely down. |
| 151 | /// Bringing the STA vif down also releases the channel context, so the |
| 152 | /// original interface survives (no `iw dev del`). |
| 153 | /// |
| 154 | /// Idempotent. Returns the monitor vif name (`<ifname>mon`). Callers pass |
| 155 | /// the returned name to channel-setting and sockets, and call |
| 156 | /// [`teardown_monitor`] with the ORIGINAL ifname to restore. |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | /// Unload + reload the kernel driver bound to `ifname`'s device, then wait |
| 171 | /// for the interface to re-enumerate and force it back DOWN (the managed |
| 172 | /// vif is recreated by the driver; NetworkManager may grab it otherwise). |
| 173 | /// Last-resort recovery for the mt76/mt7921e monitor-TX wedge: injected |
| 174 | /// frames report send() success but never radiate (verified on lab MT7922: |
| 175 | /// 12 wildcard probes → 0 probe responses; after `modprobe -r mt7921e` + |
| 176 | /// reload → 7 responses). RX is unaffected by the wedge, so scans look |
| 177 | /// healthy while deauth/PMKID silently do nothing — the only cure found is |
| 178 | /// a driver reload. ALL vifs (managed + monitor) on the phy die with the |
| 179 | /// unload; callers must recreate the monitor vif afterwards. |
| 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 | /// Reverse [`to_monitor`]: delete the monitor vif if present and bring the |
| 220 | /// original interface back up (it never left managed type). Idempotent. |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | /// Monitor vif name convention — single source of truth. |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | /// Resolve the wiphy name (e.g. "phy0") that owns `ifname` via sysfs. |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | /// Tune the interface to channel `ch` (one retry — some drivers drop the |
| 252 | /// first request when issued immediately after a mode switch). |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | /// Tune by carrier frequency — the only unambiguous way once 6 GHz enters |
| 267 | /// (6 GHz channel numbers collide with 2.4 GHz in `iw set channel`). |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | /// Wireless interfaces present: /sys/class/net/<if> has DEVTYPE=wlan in its |
| 282 | /// uevent, or a `wireless` directory. |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | /// Our own MAC on ifname (`/sys/class/net/<ifname>/address`). |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | /// True when running with euid 0; UI surfaces a warning otherwise. |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | /// True while NetworkManager is active — it will fight channel control and |
| 318 | /// pin the card to the connected network's channel (only *that* SSID's |
| 319 | /// beacons get seen). No root needed to check. |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | /// Stop NetworkManager (and with it wpa_supplicant's radio fighting). |
| 329 | /// Requires root. Restart later with [`start_networkmanager`] |
| 330 | /// (`systemctl start NetworkManager`). |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | /// Start NetworkManager again (mirrors [`stop_networkmanager`]), restoring |
| 336 | /// normal managed-mode connectivity once capture work is done. Requires root. |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | /// True when the monitor vif `<ifname>mon` no longer exists — the post-scan |
| 342 | /// restore check wants this so we can warn the user if the interface was |
| 343 | /// left in monitor mode. |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | /// True when `pkexec` (polkit) is available to elevate the privileged |
| 349 | /// service child. |
| 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 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 |