# Wifibox setup on the MacBook Air

## Purpose and topology

This setup uses the internal Broadcom BCM4360 (`pci0:3:0:0`) through a small Alpine Linux guest under bhyve. Native FreeBSD Wi-Fi uses the separate USB Realtek adapter, `rtwn0`, exposed as host `wlan0`. The guest also calls its Broadcom interface `wlan0`; these are different devices.

The host reaches the guest over `wifibox0`: host `10.0.0.2/24`, guest `10.0.0.1/24`. The guest performs NAT to its Wi-Fi connection. In secondary mode, the host has only a connected route to `10.0.0.0/24` through Wifibox. Normal traffic continues through native `wlan0`; selected destinations can be routed through Wifibox. This is not automatic failover or load balancing.

The following instructions target FreeBSD amd64 with working bhyve PCI passthrough. Check that `10.0.0.0/24` does not overlap another LAN or VPN before using it. Perform the initial hardware setup and any default-route switch from a local console. Keep the native Wi-Fi connection available during testing.

## Install the required driver-enabled guest

The generic repository `wifibox-alpine` package has `XX_DRIVER_WL` disabled. The repository `wifibox-alpine-brcm` flavor supplies open Broadcom firmware, but the image examined on this machine also lacked `wl.ko`. This BCM4360 needs the proprietary `wl` driver. Installing either image without that driver results in a guest with no wireless interface.

Install the core and build tools:

```sh
doas pkg install wifibox-core gtar mtools patchelf squashfs-tools-ng
```

A normal, current FreeBSD ports tree must provide `/usr/ports/Mk`. For a regular ports build, select `XX_DRIVER_WL` in `make config` under `net/wifibox-alpine`, using `FLAVOR=default`. Keep that selection for future rebuilds. Default firmware options can stay enabled, although they increase image size.

The exact minimal build used for this repair uses the 20260927 port snapshot and omits unrelated firmware. The port unconditionally extracts firmware `WHENCE` even when no Linux firmware was selected; the included patch guards that optional step. Run from this repository root:

```sh
mkdir -p /tmp/wifibox-port
ports_revision=dba095fc3032e7b30e5baa3396e799e359bada81
for port_file in Makefile distinfo flavors.mk pkg-plist pkg-descr pkg-message.iptables; do
  fetch -o "/tmp/wifibox-port/$port_file" \
    "https://raw.githubusercontent.com/freebsd/freebsd-ports/$ports_revision/net/wifibox-alpine/$port_file"
done
patch -d /tmp/wifibox-port < patches/wifibox/optional-firmware-whence.patch
doas make -C /tmp/wifibox-port PORTSDIR=/usr/ports FLAVOR=default BATCH=yes \
  OPTIONS_SET=XX_DRIVER_WL \
  'OPTIONS_UNSET=FW_ATH10K FW_ATH11K FW_ATH12K FW_BRCM FW_IWLWIFI FW_MEDIATEK FW_MARVELL FW_RT61 FW_RTLWIFI FW_RTW88 FW_RTW89' \
  package
```

Stop Wifibox before replacing its guest image. If installed, remove the `wifibox-brcm` metapackage and its `wifibox-alpine-brcm` image, reviewing the package transaction first. The custom package is the default flavor, named `wifibox-alpine`. Install it and protect it from replacement with a driverless repository image:

```sh
doas service wifibox onestop
# Only if these packages are installed:
doas pkg delete wifibox-brcm wifibox-alpine-brcm
# Replace an existing generic image if necessary:
doas pkg add -f /tmp/wifibox-port/work-default/pkg/wifibox-alpine-20260927.pkg
doas pkg set -A 0 wifibox-alpine wifibox-core
doas pkg lock -y wifibox-alpine
```

Keep the built package outside `/tmp` for recovery. Locking prevents automatic guest updates, including security updates: rebuild a current port with `XX_DRIVER_WL`, unlock, install the new package, verify it, and lock again. Do not simply replace it with the repository generic or `brcm` image. The Wifibox core can continue receiving normal updates.

## Configure PCI passthrough

Confirm the device rather than assuming the PCI address on another machine:

```sh
pciconf -lv
```

In `/boot/loader.conf`, preserve existing settings and add these if absent:

```conf
vmm_load="YES"
hw.vmm.ppt_devs="3/0/0"
```

In `/etc/rc.conf`, preserve other entries and block native drivers for the internal Broadcom device:

```conf
devmatch_enable="YES"
devmatch_blocklist="if_brcmfmac if_bwn if_bwi"
```

If a blocklist already exists, append these names rather than overwriting unrelated entries. Keep the USB Realtek driver enabled. Reboot from a console if new loader settings are needed. Wifibox also attaches the configured PCI device to `ppt` at startup; do not pass through the device currently carrying your remote session.

## Configure the guest and credentials

Package sample files live under `/usr/local/etc/wifibox`. Preserve existing files; copy a `.sample` only when its corresponding configuration is absent. Back up existing configurations to a root-only directory before changing them. Sample appliance settings normally already provide guest `eth0` at `10.0.0.1/24`, guest wireless DHCP, DHCP/NAT, and socket forwarding.

Set these values in `/usr/local/etc/wifibox/bhyve.conf`:

```conf
cpus=1
memory=128M
console=no
passthru=3/0/0
priority=50
stop_wait_max=30
```

The guest supplicant file is `/usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf`; the `app_config` symlink must point to `wpa_supplicant`. Edit the file as root without putting a passphrase on a command line or into this repository:

```conf
ctrl_interface=/var/run/wifibox
ctrl_interface_group=0
update_config=1
network={
  ssid="YOUR_SSID"
  psk="YOUR_PASSPHRASE"
}
```

Protect it with `doas chmod 600 /usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf`. Existing working profiles can be retained.

In `/usr/local/etc/wifibox/appliance/uds_passthru.conf`, retain the configured ports and permissions, and set:

```sh
network=10.0.0.1:255.255.255.0
_sockdir=/var/run/wifibox
_perms="user=root,group=0,mode=770"
sockets="path=${_sockdir}/wlan0,${_perms},port=1200
path=${_sockdir}/p2p-dev-wlan0,${_perms},port=1201"
```

Both guest and host forwarding use these socket paths. Keeping them under `/var/run/wifibox` avoids overwriting native FreeBSD `/var/run/wpa_supplicant/wlan0`. The host Wifibox runtime directory is supplied by the package. Use `wpa_cli -p /var/run/wifibox -i wlan0` for Wifibox and plain `wpa_cli -i wlan0` for native Wi-Fi.

## Enable secondary mode

Preserve the native adapter's existing `WPA DHCP` settings. Configure the Wifibox host side statically, so its DHCP server cannot change the host gateway or resolver:

```sh
doas sysrc wifibox_enable=YES
doas sysrc ifconfig_wifibox0='inet 10.0.0.2/24 up'
doas sysrc background_dhclient_wifibox0=NO
```

Remove only Wifibox's old default-route configuration. On this machine it was `route_wifiboxgw="-net 0.0.0.0/0 10.0.0.1"` with `wifiboxgw` in `static_routes`. Remove that token while preserving other static routes, delete `route_wifiboxgw`, and remove `defaultrouter="10.0.0.1"` if present. Do not enable IPv6 router-advertisement acceptance on `wifibox0` in secondary mode, because it could install an IPv6 default route.

Start only the relevant service and interface:

```sh
doas service wifibox start
doas service netif start wifibox0
```

Allow about 30 seconds for guest initialization. After a full `service wifibox restart`, reapply its host address with `service netif start wifibox0`. At boot, Wifibox starts before `netif`, which applies the static address. Avoid restarting all host networking while connected remotely.

Check the guest, association, and unchanged primary route:

```sh
doas service wifibox status
ifconfig wifibox0
ping -c 3 -t 5 10.0.0.1
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 status
route -n get default
```

Supplicant should report `wpa_state=COMPLETED` and a guest wireless IP address. The host default should still name native `wlan0`.

Test an otherwise unused Internet destination through Wifibox with a temporary host route. Check first that no route already exists for that destination, and always remove the test route afterward:

```sh
doas route add -host 1.1.1.1 10.0.0.1
route -n get 1.1.1.1
ping -c 3 -t 8 1.1.1.1
fetch -T 10 -o /dev/null https://1.1.1.1/cdn-cgi/trace
doas route delete -host 1.1.1.1 10.0.0.1
route -n get default
```

The test route must name `wifibox0`; successfully starting a VM alone does not establish a working Internet connection.

## Connect Wifibox to a new SSID

Use Wifibox's forwarded socket explicitly in every command: `-p /var/run/wifibox -i wlan0`. Plain `wpa_cli -i wlan0` controls the native USB adapter. Switching Wifibox SSIDs interrupts traffic using Wifibox; in secondary mode, the native default connection continues to carry normal traffic.

### Discover networks

Request a scan, wait a few seconds, and read the results:

```sh
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 scan
# Run this after the scan has had time to finish:
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 scan_results
```

`scan` should reply `OK`. Results are asynchronous: if the list is stale, wait and request `scan_results` again. Scanning can briefly interrupt traffic on the guest's radio. It does not change the host default route.

### Add and save the profile

Back up the current guest configuration and edit it as root:

```sh
doas cp -p /usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf \
  /usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf.before-ssid-change
doas vi /usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf
```

Keep the existing global settings and saved network blocks. Append a block for a WPA/WPA2 personal network, replacing the placeholders in the editor:

```conf
network={
  ssid="NEW_SSID"
  psk="NEW_PASSPHRASE"
}
```

This file is mounted into the guest, so editing it saves the profile across guest restarts. Keep credentials out of shell arguments and history. Escape literal double quotes and backslashes in quoted configuration values. For a hidden SSID, add `scan_ssid=1` inside its block. For an open network, use `key_mgmt=NONE` instead of the `psk` line; enterprise and WPA3-only networks need a different configuration and hardware/driver support.

Reload the file and list the profiles:

```sh
doas chmod 600 /usr/local/etc/wifibox/wpa_supplicant/wpa_supplicant.conf
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 reconfigure
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 list_networks
```

Require an `OK` reply from `reconfigure`; `FAIL` or a timeout means the reload has not succeeded. Read the numeric ID beside `NEW_SSID` in the latest list. IDs can change after a reload. To select it, replace the example `1` below with that ID:

```sh
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 select_network 1
```

`select_network` disables other profiles in the running supplicant while it tries this one. It does not delete their saved blocks. The profile is already saved by the file edit; avoid `save_config` after selecting if you want to preserve the other profiles' enabled state on disk.

### Confirm the connection and recover if necessary

Allow association and guest DHCP to finish, then check:

```sh
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 status
route -n get default
```

Require the intended `ssid`, `wpa_state=COMPLETED`, and a guest `ip_address`. An `OK` response to selection only acknowledges the request. Guest DHCP handles the new wireless address; do not run host `dhclient wifibox0`, which would undermine the static secondary configuration. Verify Internet traffic using the temporary destination-route test in the secondary-mode section.

To switch back, run `list_networks` again and `select_network` with the previous SSID's current ID. To restore automatic choice among all saved profiles:

```sh
doas timeout 10 wpa_cli -p /var/run/wifibox -i wlan0 enable_network all
```

For a persistent preference, add `priority=10` to the preferred network block (higher values win among eligible profiles; the default is zero), then reload. Remove or reduce that value to stop preferring it. To undo a faulty file edit, restore the `.before-ssid-change` backup and reload with `reconfigure`. Inspect the guest `messages` log if authentication or DHCP fails. A supplicant reload is sufficient for normal profile changes; a VM restart is unnecessary.

These profile-changing instructions were checked against the installed CLI and upstream control-interface documentation. No live SSID switch was performed while documenting them.

## Make Wifibox the default connection later

This section describes an optional switch; it was not applied during the secondary-mode repair. Perform it from a console after the tests above pass. Record the previous default gateway and DNS settings first.

To switch the current IPv4 default without rebooting:

```sh
doas route change default 10.0.0.1
route -n get default
```

If no default route exists, use `route add default 10.0.0.1`. Native LAN routes remain connected to `wlan0`, so traffic to that LAN can still use the native adapter. A default-route switch affects new Internet traffic and can interrupt remote sessions.

For a persistent setup, set Wifibox as the static default and prevent the native adapter's DHCP client from installing a competing default during boot. On this machine, the simple choice is to keep native association enabled but stop automatically requesting an address:

```sh
doas sysrc defaultrouter=10.0.0.1
doas sysrc ifconfig_wlan0=WPA
```

This leaves native Wi-Fi available for manual recovery but does not automatically retain its DHCP address after reboot. For simultaneous addressed connections with Wifibox as default, configure a deliberate native static address or a reviewed DHCP hook that suppresses native route and resolver changes; do not use two uncontrolled DHCP defaults.

Configure DNS separately: a static Wifibox host address does not import the guest's DHCP DNS. Choose DNS servers reachable through the new default, and preserve the current resolver configuration. If using FreeBSD `resolvconf`, merge a `name_servers` setting into `/etc/resolvconf.conf`, for example `name_servers="1.1.1.1 9.9.9.9"`, then run `doas resolvconf -u`. Keep required local search domains and private DNS servers appropriate to your network. Verify both numeric Internet access and hostname lookup after the switch.

To return to native Wi-Fi on this machine:

```sh
doas sysrc -x defaultrouter
doas sysrc ifconfig_wlan0='WPA DHCP'
doas service netif start wlan0
# If it already has its DHCP address, restore the recorded gateway directly:
doas route change default 192.168.3.1
```

Restore any DNS changes separately and confirm `route -n get default` names `wlan0`. The recorded `192.168.3.1` gateway is specific to the current LAN.

## Troubleshooting and recovery

Inspect `/var/log/wifibox.log` for host startup errors and `/var/run/wifibox/appliance/log/messages` and `dmesg` for guest failures. `Could not find a wireless interface` plus a guest image without `wl.ko` indicates the wrong image for this BCM4360. Verify driver inclusion using `unsquashfs -ll /usr/local/share/wifibox/root.img | rg 'wl\.ko'`; the minimal custom package records `XX_DRIVER_WL: on` in `pkg info wifibox-alpine`.

For guest console diagnostics, set `console=yes` in `bhyve.conf`, restart Wifibox, reapply the host interface configuration, and use `doas wifibox console`. Starting a guest or receiving ARP replies does not prove association or NAT works. Avoid dumping supplicant configuration into logs because it contains credentials.

The repair's root-only configuration backup is `/root/wifibox-secondary-backup-20261009-100011`. It contains the previous `rc.conf`, `bhyve.conf`, socket-forwarding configuration, and supplicant configuration. Restore individual settings carefully: the old configuration includes the default-route entry and the conflicting socket path, so restoring it wholesale would undo the secondary setup.

## Verification on 2026-10-09

Installed `wifibox-core-0.17.0` with the locally built `wifibox-alpine-20260927`, `XX_DRIVER_WL` enabled and Linux LTS `6.18.54`. The guest associated with `MC_Leo` at 5765 MHz and obtained `192.168.3.226`. Host-to-guest ping passed with no loss. A temporary host route to `1.1.1.1` through `10.0.0.1` selected `wifibox0`; three Internet pings passed and an HTTPS fetch completed. The test route was removed, and the host default remained `192.168.3.1` through native `wlan0`. The custom guest package is locked and a recovery copy is saved as `/root/wifibox-secondary-backup-20261009-100011/wifibox-alpine-20260927-wl.pkg`.

These are live connection checks. Boot ordering and the optional persistent default switch are documented from rc.d configuration and were not tested by rebooting this machine. The proprietary driver emits a warning that it is unmaintained and incompatible with some Linux security mitigations; using a supported replacement adapter avoids that driver limitation.

## References

The [upstream supplicant control-interface documentation](https://w1.fi/wpa_supplicant/devel/ctrl_iface_page.html) describes scanning, reloads, profile selection, and enabling saved networks.

See the installed `wifibox(8)` and `wifibox-alpine(5)` manual pages, the [Wifibox source](https://github.com/pgj/freebsd-wifibox), the [Alpine guest source](https://github.com/pgj/freebsd-wifibox-alpine), and the [exact FreeBSD port snapshot used here](https://github.com/freebsd/freebsd-ports/tree/dba095fc3032e7b30e5baa3396e799e359bada81/net/wifibox-alpine). The [FreeBSD manual](https://man.freebsd.org/cgi/man.cgi?manpath=freebsd-release-ports&query=wifibox&sektion=8) describes static host addressing and service ordering; this setup deliberately omits its example default-gateway setting in secondary mode.
