# 實驗: WiFi 的無線設定 (iw)

## 取得連線裝置

當我們需要取得連線裝置的資訊，我們可以用以下四個指令:&#x20;

1. arp&#x20;
2. cat /tmp/dhcp.leases
3. iwinfo wlan assoclist
4. iw wlan1 station dump | grep Station

第一個指令是取得 ARP 的對應表。ARP (Address Resolution Protocol) 是用以進行 IP 和 MAC 位址的轉換表格。考慮到在 WLAN下，必須執行封包的轉發，所以在 WLAN 下的使用者都會出現在 ARP 表格中。

第二種方法則是取得 DHCP 的租約資訊，存放於`/tmp/dhcp.leases`，考慮到WiFi AP同時扮演無線網路的路由器 (同時扮演 DHCP 伺服器的角色)，因此，所有的連線裝置都會註冊 DHCP 租約。以下為執行結果。

![arp / dhcp.leases](/files/-LNogUkJ8Yc-bbhwl3bP)

這兩個方法都有各自的缺點。ARP 會取得 WLAN 之外的資訊，例如，在上圖中，就出現透過有線連上的裝置。DHCP 租約則有可能出現之前連線上的裝置。為了解決此問題，我們介紹在 OpenWRT 中一個很有用工具: `iw` 和 `iwinfo`。

```
iwinfo wlan assoclist
```

![iwinfo wlan assoclist](/files/-LNojiTyRiA0oJtxmrof)

`iwinfo` 指令在之前實驗中已介紹。`iwinfo` 可以取得無線網路介面的名稱以及各種設定。在此，也可以取得無線網路下的連線裝置清單，以及許多細節，包含裝置的傳送速率、MCS 模式與封包數等資訊。

```
iw wlan1 station dump | grep Station
```

`iw [wifi device] station dump` 則會顯示連線裝置的各種資訊。而 `| grep Station` 的用意則是只留下 Station 的資訊，也就是連線的裝置 (station)，我們會在"傳輸封包的統計資訊"這個章節中詳細介紹此指令。

```
root@GL-AR750S:~# iw wlan1 station dump | grep Station
Station ec:22:80:0e:c5:91 (on wlan1)
Station 82:3d:ba:58:75:d6 (on wlan1)
Station 80:7a:bf:6d:a0:37 (on wlan1)
```

## 改變傳輸 MCS 模式

在 OpenWRT 的架構下，透過指令，我們也可以更改連線裝置的傳輸速率，並透過 `iwinfo` 查看改變的結果。當我們要設定 MCS 模式時，使用`iw`指令，直接對無線網路介面操作，指令如下:

```
iw dev [wlan0] set bitrates ht-mcs-2.4 [10]
```

\[wlan0] 為無線網卡的代號，可以用`iwinfo`查詢，請參考實驗二的內容。\[10] 則填入MCS模式，可填入的範圍受到硬體的限制，對於所使用的裝置而言，可設定範圍為 0-15，此限制是來自於天線數量的限制，由於只有兩根天線，所以最多可使用15。

當設定完後，我們用`iwinfo`可以看到 MCS 模式的確產生改變。不過，此改變只有在封包傳輸後才更新。以下為執行結果:

![MCS測試 (單使用者)](/files/-LNop1XXrnOhnpnxEjYz)

然而，此設定會影響到所有的無線網路使用者。我們可以參考以下的設定結果。

![MCS測試 (多使用者)](/files/-LNopoYITuatAUSKWv_h)

\
在上圖，我們可以看到有三個使用者的 TX MCS 都被調整到 10，MCS 為 0 的使用者沒有進行資料連線，所以未更新資訊。目前還沒有辦法針對單一使用者調整 MCS 模式，同時，MCS 模式也會在傳輸過程中自動被WiFi網卡根據訊號強度 (RSSI) 來校正。因此，就算我們調整了 MCS 模式，在不久之後，也會被自動調整為其他數值。

{% hint style="info" %}
什麼是MCS模式?

MCS (Modulation and Coding Scheme) 為一組調整傳送速率的參數。根據不同的設定，包含:使用幾根天線傳送、調變技術、以及編碼技術，來調整不同的傳輸速率。一般來說，MCS 數值越高代表越高的傳送速率，舉例來說，MCS 為 15 時，在 40MHz 的頻寬下，802.11n 可以到達 300MBps 的速率。在 WiFi 通訊中，MCS 模式的調整，是根據環境中的 SNR (Signal-to-Noise Ratio) 數值來調整。MCS 模式的對應關係可以參考以下連結:

* <https://en.wikipedia.org/wiki/IEEE_802.11n-2009>
* <http://mcsindex.com/>
  {% endhint %}

## 傳輸封包的統計資訊

在之前的實驗中介紹了如何透過`iw` 指令來取得連線裝置資訊，事實上，`iw`指令可以取得更多的通訊資訊，以下為測試的結果:

```
root@GL-AR750S:~# iw wlan1 station dump
Station 80:7a:bf:6d:a0:37 (on wlan1)
        inactive time:  250 ms
        rx bytes:       3490445
        rx packets:     25609
        tx bytes:       67131488
        tx packets:     51677
        tx retries:     4856
        tx failed:      67
        rx drop misc:   36
        signal:         -34 [-41, -34] dBm
        signal avg:     -35 [-41, -37] dBm
        tx bitrate:     144.4 MBit/s MCS 15 short GI
        rx bitrate:     24.0 MBit/s
        expected throughput:    46.875Mbps
        authorized:     yes
        authenticated:  yes
        associated:     yes
        preamble:       short
        WMM/WME:        yes
        MFP:            no
        TDLS peer:      no
        DTIM period:    2
        beacon interval:100
        short preamble: yes
        short slot time:yes
        connected time: 8549 seconds
Station a4:02:b9:cd:70:c6 (on wlan1)
        inactive time:  240 ms
        rx bytes:       1223857
        rx packets:     3480
        tx bytes:       3827401
        tx packets:     4202
        tx retries:     1198
        tx failed:      24
        rx drop misc:   11
        signal:         -27 [-32, -29] dBm
        signal avg:     -25 [-32, -27] dBm
        tx bitrate:     57.8 MBit/s MCS 5 short GI
        rx bitrate:     72.2 MBit/s MCS 7 short GI
        expected throughput:    29.21Mbps
        authorized:     yes
        authenticated:  yes
        associated:     yes
        preamble:       short
        WMM/WME:        yes
        MFP:            no
        TDLS peer:      no
        DTIM period:    2
        beacon interval:100
        short preamble: yes
        short slot time:yes
        connected time: 121 seconds 
```

可以看到，透過 `iw wlan1 station dump`可以取得許多通訊上的統計值。`iw`指令對於處於 station 或是 master 模式下的介面才能操作，因此，無法取得監聽的資訊。若是要取得特定裝置的統計資訊，可以利用 MAC 位址進行查詢。

```
root@GL-AR750S:~# iw wlan1 station get ec:22:80:0e:c5:91
Station ec:22:80:0e:c5:91 (on wlan1)
        inactive time:  0 ms
        rx bytes:       2326941
        rx packets:     23395
        tx bytes:       1217689
        tx packets:     7658
        tx retries:     520
        tx failed:      14
        rx drop misc:   58
        signal:         -46 [-47, -51] dBm
        signal avg:     -45 [-46, -50] dBm
        tx bitrate:     135.0 MBit/s MCS 6 40MHz short GI
        rx bitrate:     150.0 MBit/s MCS 7 40MHz short GI
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openwrt-nctu.gitbook.io/project/openwrt-functions/openwrt-iw-command.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
