> For the complete documentation index, see [llms.txt](https://www.axisnow.io/zh/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.axisnow.io/zh/docs/edge/policies/real-ip/toa.md).

# 安装 TOA 内核模块

在源站安装 TOA 内核模块以透明获取真实客户端 IP

TOA（TCP Option Address）把真实客户端 IP 和端口放在 TCP 选项中传给源站。源站加载 TOA 内核模块后，应用通过 `getpeername` 等常规接口获得的即为真实客户端地址，无需改动程序，也不需要源站软件支持 Proxy Protocol。模块使用 [ucloud/ucloud-toa](https://github.com/ucloud/ucloud-toa)，支持 2.6.32 到当前主线内核。

## 前提 <a href="#prerequisites" id="prerequisites"></a>

* 源站为 Linux；只对 TCP 有效，只传递 IPv4 客户端地址，源站监听 IPv6 地址（如 Go 默认的 `::`）时获得的是 IPv4 映射地址。
* 编译需要与运行内核完全一致版本的内核头文件；内核升级后要重新编译并加载。
* TCP 选项没有防篡改保护。只让边缘的流量到达源站端口，否则直连源站的客户端可以伪造来源地址。

## 安装 <a href="#install" id="install"></a>

1. 安装编译工具和内核头文件：

```bash
# CentOS / RHEL
sudo yum install -y gcc make kernel-devel-$(uname -r)
# Ubuntu / Debian
sudo apt-get install -y gcc make linux-headers-$(uname -r)
# SUSE
sudo zypper install gcc make kernel-default-devel
```

2. 获取源码并编译，得到 `toa.ko`：

```bash
git clone https://github.com/ucloud/ucloud-toa.git
cd ucloud-toa
make
```

提示 `config_retpoline=y but not supported by the compiler` 说明 gcc 版本过低，升级后重试。

3. 加载并确认：

```bash
sudo insmod toa.ko
lsmod | grep toa
dmesg | grep -i toa     # 看到 TOA: toa loaded
```

4. 开机自动加载。把模块放进内核模块目录并登记，然后用 `modprobe` 确认登记成功：

```bash
sudo mkdir -p /lib/modules/$(uname -r)/extra
sudo cp toa.ko /lib/modules/$(uname -r)/extra/
sudo depmod -a
echo toa | sudo tee /etc/modules-load.d/toa.conf
sudo rmmod toa && sudo modprobe toa
```

源站内核需要定期升级时，第 2 到 4 步可改用 DKMS，内核升级后自动重新编译并加载：

```bash
sudo cp -r ucloud-toa /usr/src/toa-1.0.0
sudo dkms add -m toa -v 1.0.0
sudo dkms install -m toa -v 1.0.0
echo toa | sudo tee /etc/modules-load.d/toa.conf
```

## 验证 <a href="#verify" id="verify"></a>

在 AxisNow 的 TCP 转发端点上把[转发方式设为 TOA](/zh/docs/edge/policies/real-ip/use-cases.md#tcp)，从外部访问一次，然后在源站查看模块计数，`syn_recv_sock_toa` 和 `getname_toa_ok` 随访问增长即为生效：

```bash
cat /proc/net/toa_stats
```

应用日志里的来源地址此时应为客户端真实 IP。`ss`、`netstat` 显示的仍是边缘地址，模块只改变应用通过 `getpeername` 获得的结果。

## 卸载 <a href="#uninstall" id="uninstall"></a>

```bash
sudo rmmod toa
sudo rm /etc/modules-load.d/toa.conf /lib/modules/$(uname -r)/extra/toa.ko
sudo depmod -a
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.axisnow.io/zh/docs/edge/policies/real-ip/toa.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
