> For the complete documentation index, see [llms.txt](https://handbook.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://handbook.bsdcn.org/di-35-zhang-gao-ji-wang-luo/35.5.-wu-xian-dian-dui-dian-mo-shi.md).

# 35.5.无线自组织（Ad-hoc）模式

IBSS 模式，也叫做无线自组织（Ad-hoc）模式，是为点对点连接设计的。例如，为在机器 `A` 和 `B` 之间建立 ad-hoc 网络，需要选择两个 IP 地址和一个 SSID。

在 `A` 上：

```sh
# ifconfig wlan0 create wlandev ath0 wlanmode adhoc
# ifconfig wlan0 inet 192.168.0.1 netmask 255.255.255.0 ssid freebsdap
# ifconfig wlan0
  wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	  ether 00:11:95:c3:0d:ac
	  inet 192.168.0.1 netmask 0xffffff00 broadcast 192.168.0.255
	  media: IEEE 802.11 Wireless Ethernet autoselect mode 11g <adhoc>
	  status: running
	  ssid freebsdap channel 2 (2417 Mhz 11g) bssid 02:11:95:c3:0d:ac
	  country US ecm authmode OPEN privacy OFF txpower 21.5 scanvalid 60
	  protmode CTS wme burst
```

`adhoc` 参数表示该接口正在运行在 IBSS 模式下。

`B` 现在应该能检测到 `A`：

```sh
# ifconfig wlan0 create wlandev ath0 wlanmode adhoc
# ifconfig wlan0 up scan
  SSID/MESH ID    BSSID              CHAN RATE   S:N     INT CAPS
  freebsdap       02:11:95:c3:0d:ac    2   54M -64:-96  100 IS   WME
```

输出中的 `I` 确认了 `A` 处于 ad-hoc 模式。现在，为 `B` 配置一个不同的 IP 地址：

```sh
# ifconfig wlan0 inet 192.168.0.2 netmask 255.255.255.0 ssid freebsdap
# ifconfig wlan0
  wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	  ether 00:11:95:d5:43:62
	  inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
	  media: IEEE 802.11 Wireless Ethernet autoselect mode 11g <adhoc>
	  status: running
	  ssid freebsdap channel 2 (2417 Mhz 11g) bssid 02:11:95:c3:0d:ac
	  country US ecm authmode OPEN privacy OFF txpower 21.5 scanvalid 60
	  protmode CTS wme burst
```

现在，`A` 和 `B` 都可以交换信息了。

## 35.5.1. FreeBSD 主机接入点

FreeBSD 可以作为接入点 (AP)，这样就不需要购买硬件接入点或运行 ad-hoc 网络。当 FreeBSD 机器作为其他网络（例如互联网）的网关时，这尤其有用。

### 35.5.1.1. 基本设置

在将 FreeBSD 机器配置为接入点之前，必须在内核中配置适当的无线网卡网络支持和所使用的安全协议。有关详细信息，请参见 [基本设置](#35511-基本设置)。

> **注意**
>
> 当前 Windows® 驱动程序的 NDIS 驱动程序包装器不支持 AP 操作。仅原生的 FreeBSD 无线驱动程序支持 AP 模式。

加载无线网络支持后，检查无线设备是否支持基于主机的接入点模式，也称为 hostap 模式：

```sh
# ifconfig wlan0 create wlandev ath0
# ifconfig wlan0 list caps
drivercaps=6f85edc1<STA,FF,TURBOP,IBSS,HOSTAP,AHDEMO,TXPMGT,SHSLOT,SHPREAMBLE,MONITOR,MBSS,WPA1,WPA2,BURST,WME,WDS,BGSCAN,TXFRAG>
cryptocaps=1f<WEP,TKIP,AES,AES_CCM,TKIPMIC>
```

此输出显示了网卡的功能。`HOSTAP` 关键字确认该无线卡可以作为接入点。还列出了各种支持的加密协议：WEP、TKIP 和 AES。此信息指示了可用于接入点的安全协议。

无线设备只能在创建网络伪设备时切换到 hostap 模式，因此必须首先销毁已创建的设备：

```sh
# ifconfig wlan0 destroy
```

然后使用正确的选项重新创建设备，并设置其他参数：

```sh
# ifconfig wlan0 create wlandev ath0 wlanmode hostap
# ifconfig wlan0 inet 192.168.0.1 netmask 255.255.255.0 ssid freebsdap mode 11g channel 1
```

再次使用 [ifconfig(8)](https://man.freebsd.org/cgi/man.cgi?query=ifconfig\&sektion=8\&format=html) 查看 **wlan0** 接口的状态：

```sh
# ifconfig wlan0
  wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	  ether 00:11:95:c3:0d:ac
	  inet 192.168.0.1 netmask 0xffffff00 broadcast 192.168.0.255
	  media: IEEE 802.11 Wireless Ethernet autoselect mode 11g <hostap>
	  status: running
	  ssid freebsdap channel 1 (2412 Mhz 11g) bssid 00:11:95:c3:0d:ac
	  country US ecm authmode OPEN privacy OFF txpower 21.5 scanvalid 60
	  protmode CTS wme burst dtimperiod 1 -dfs
```

`hostap` 参数表示该接口正在运行在基于主机的接入点模式下。

通过将以下行添加到 **/etc/rc.conf**，可以在引导时自动配置接口：

```sh
wlans_ath0="wlan0"
create_args_wlan0="wlanmode hostap"
ifconfig_wlan0="inet 192.168.0.1 netmask 255.255.255.0 ssid freebsdap mode 11g channel 1"
```

### 35.5.1.2. 无认证或加密的基于主机的接入点

尽管不建议在没有任何认证或加密的情况下运行接入点（AP），但这是检查接入点是否正常工作的简单方式。此配置对于调试客户端问题也非常重要。

配置好接入点后，从另一台无线机器发起扫描以查找该接入点：

```sh
# ifconfig wlan0 create wlandev ath0
# ifconfig wlan0 up scan
SSID/MESH ID    BSSID              CHAN RATE   S:N     INT CAPS
freebsdap       00:11:95:c3:0d:ac    1   54M -66:-96  100 ES   WME
```

客户端机器找到了接入点，并且可以与其关联：

```sh
# ifconfig wlan0 inet 192.168.0.2 netmask 255.255.255.0 ssid freebsdap
# ifconfig wlan0
  wlan0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
	  ether 00:11:95:d5:43:62
	  inet 192.168.0.2 netmask 0xffffff00 broadcast 192.168.0.255
	  media: IEEE 802.11 Wireless Ethernet OFDM/54Mbps mode 11g
	  status: associated
	  ssid freebsdap channel 1 (2412 Mhz 11g) bssid 00:11:95:c3:0d:ac
	  country US ecm authmode OPEN privacy OFF txpower 21.5 bmiss 7
	  scanvalid 60 bgscan bgscanintvl 300 bgscanidle 250 roam:rssi 7
	  roam:rate 5 protmode CTS wme burst
```

### 35.5.1.3. WPA2 基于主机的接入点

本节重点介绍如何使用 WPA2 安全协议设置 FreeBSD 接入点。有关 WPA 和基于 WPA 的无线客户端配置的更多详细信息，请参阅 [使用 EAP-TLS 的 WPA](/di-35-zhang-gao-ji-wang-luo/35.4.-wu-xian-gao-ji-shen-fen-yan-zheng.md)。

[hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html) 守护进程用于处理客户端认证和 WPA2 启用接入点的密钥管理。

以下配置操作是在作为接入点的 FreeBSD 机器上执行的。接入点正确工作后，可以通过在 **/etc/rc.conf** 中添加以下行来自动启动 [hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html) 守护进程：

```sh
hostapd_enable="YES"
```

在尝试配置 [hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html) 之前，首先配置 [基本设置](#35511-基本设置) 中介绍的基本设置。

#### 35.5.1.3.1. WPA2-PSK

WPA2-PSK 适用于无法或不希望使用后端认证服务器的小型网络。

配置在 **/etc/hostapd.conf** 中完成：

```sh
interface=wlan0                 ①
debug=1                         ②
ctrl_interface=/var/run/hostapd ③
ctrl_interface_group=wheel      ④
ssid=freebsdap                  ⑤
wpa=2                           ⑥
wpa_passphrase=freebsdmall      ⑦
wpa_key_mgmt=WPA-PSK            ⑧
wpa_pairwise=CCMP               ⑨
```

* ① 用于接入点的无线接口。
* ② 执行 [hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html) 时使用的详细程度。值 `1` 表示最小级别。
* ③ [hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html) 用于存储域套接字文件的目录路径，用于与外部程序（如 [hostapd\_cli(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd_cli\&sektion=8\&format=html)）通信。此示例使用默认值。
* ④ 允许访问控制接口文件的组。
* ⑤ 无线网络名称或 SSID，它将在无线扫描中显示。
* ⑥ 启用 WPA 并指定所需的 WPA 认证协议。值 `2` 配置接入点为 WPA2，推荐使用。仅在需要过时的 WPA 时设置为 `1`。
* ⑦ 用于 WPA 认证的 ASCII 密码短语。
* ⑧ 要使用的密钥管理协议。此示例设置为 WPA-PSK。
* ⑨ 接入点接受的加密算法。在此示例中，仅接受 CCMP（AES）密码。CCMP 是 TKIP 的替代方案，在可能的情况下强烈推荐使用。只有在有站点无法使用 CCMP 时，才应允许使用 TKIP。

接下来的步骤是启动 [hostapd(8)](https://man.freebsd.org/cgi/man.cgi?query=hostapd\&sektion=8\&format=html)：

```sh
# service hostapd forcestart
```

```sh
# ifconfig wlan0
wlan0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
	ether 04:f0:21:16:8e:10
	inet6 fe80::6f0:21ff:fe16:8e10%wlan0 prefixlen 64 scopeid 0x9
	nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
	media: IEEE 802.11 Wireless Ethernet autoselect mode 11na <hostap>
	status: running
	ssid No5ignal channel 36 (5180 MHz 11a ht/40+) bssid 04:f0:21:16:8e:10
	country US ecm authmode WPA2/802.11i privacy MIXED deftxkey 2
	AES-CCM 2:128-bit AES-CCM 3:128-bit txpower 17 mcastrate 6 mgmtrate 6
	scanvalid 60 ampdulimit 64k ampdudensity 8 shortgi wme burst
	dtimperiod 1 -dfs
	groups: wlan
```

接入点启动后，客户端可以与其关联。有关更多详细信息，请参阅 [基本设置](#35511-基本设置)。可以使用 `ifconfig wlan0 list sta` 查看与接入点关联的站点。
