自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
網路堆疊設定工具
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 作業系統包含標準 Linux 網路公用程式,例如 ifconfig
、ip
和 ip6tables
。這些公用程式位於系統映像檔中,可啟用整個 Linux 網路堆疊的設定。在搭載 Android 7.x 以下版本的裝置上,供應商程式碼可直接呼叫這些二進位檔,但會導致下列問題:
- 由於網路公用程式是在系統映像檔中更新,因此無法提供穩定的實作方式。
- 網路公用程式的範圍非常廣泛,因此很難在保證可預測行為的同時,改進系統映像檔。
在搭載 Android 8.0 以上版本的裝置上,系統分區會收到更新,但供應商分區則保持不變。為達成此目標,Android 8.0 提供定義穩定版本介面的功能,同時使用 SELinux 限制,將供應商和系統映像檔的互相依賴性維持在已知良好的組合中。
供應商可以使用平台提供的網路設定公用程式,設定 Linux 網路堆疊,但這些公用程式尚未包含 HIDL 介面包裝函式。為定義這類介面,Android 8.0 包含 netutils-wrapper-1.0
工具。
Netutils 包裝函式
netutils
包裝函式公用程式提供 Linux 網路堆疊設定的子集,不會受到系統分區更新的影響。Android 8.0 包含 1.0 版的包裝函式,可讓您傳遞與包裝公用程式相同的引數,這些公用程式會在 /system/bin
的系統分區中安裝,如下所示:
u:object_r:system_file:s0 /system/bin/ip-wrapper-1.0 -> netutils-wrapper-1.0
u:object_r:system_file:s0 /system/bin/ip6tables-wrapper-1.0 -> netutils-wrapper-1.0
u:object_r:system_file:s0 /system/bin/iptables-wrapper-1.0 -> netutils-wrapper-1.0
u:object_r:system_file:s0 /system/bin/ndc-wrapper-1.0 -> netutils-wrapper-1.0
u:object_r:netutils_wrapper_exec:s0 /system/bin/netutils-wrapper-1.0
u:object_r:system_file:s0 /system/bin/tc-wrapper-1.0 -> netutils-wrapper-1.0
Symlinks 會顯示由 netutils
包裝函式包裝的網路公用程式,包括:
ip
iptables
ip6tables
ndc
tc
如要在 Android 8.0 以上版本中使用這些公用程式,供應商實作必須遵守下列規則:
- 供應商程序不得直接執行
/system/bin/netutils-wrapper-1.0
;嘗試執行這項操作會導致錯誤。
netutils-wrapper-1.0
包裝的所有公用程式都必須使用其符號連結啟動。例如,將先前執行此操作的供應商程式碼 (/system/bin/ip <FOO> <BAR>
) 變更為 /system/bin/ip-wrapper-1.0 <FOO> <BAR>
。
- 平台 SELinux 政策禁止在沒有網域轉換的情況下執行包裝函式。此規則不得變更,且會在 Android Compatibility Test Suite (CTS) 中接受測試。
- 直接執行公用程式 (例如
/system/bin/ip <FOO> <BAR>
) 的行為。這項規則不得變更,且會在 CTS 中進行測試。
- 任何需要啟動包裝函式的供應商網域 (程序),都必須在 SELinux 政策中新增下列網域轉換規則:
domain_auto_trans(VENDOR-DOMAIN-NAME, netutils_wrapper_exec,
netutils_wrapper)
。
Netutils 包裝函式篩選器
包裝的實用工具可用於設定 Linux 網路堆疊的幾乎所有層面。不過,為確保系統能維持穩定的介面,並允許更新系統分區,我們只允許特定的指令列引數組合;其他指令將遭到拒絕。
供應商介面和鏈結
這個包裝函式具有供應商介面的概念。這些介面通常由供應商程式碼管理,例如行動數據介面。通常,其他類型的介面 (例如 Wi-Fi) 會由 HAL 和架構管理。這個包裝函式會根據名稱 (使用規則運算式) 辨識供應商介面,並允許供應商程式碼對這些介面執行多項作業。目前供應商介面如下:
- 名稱結尾為「oem」且後面接數字的介面,例如
oem0
或 r_oem1234
。
- 目前的 SOC 和 OEM 實作項目 (例如
rmnet_data[0-9]
) 所使用的介面。
通常由架構管理的介面名稱 (例如 wlan0
) 絕不會是供應商介面。
包裝函式與供應商鏈的概念類似。這些值會用於 iptables
指令,並可透過名稱辨識。目前供應商鏈:
- 出發時為
oem_
。
- 目前的 SOC 和 OEM 實作會使用這些鏈結,例如以
nm_
或 qcom_
開頭的鏈結。
允許的指令
目前允許的指令如下所列。限制會透過執行命令列上的一組規則運算式實作。詳情請參閱 system/netd/netutils_wrappers/NetUtilsWrapper-1.0.cpp
。
ip
ip
指令用於設定 IP 位址、路由、IPsec 加密和其他多項網路參數。這個包裝函式可允許下列指令:
- 在供應商管理介面中新增及移除 IP 位址。
- 設定 IPsec 加密。
iptables 和 ip6tables
iptables
和 ip6tables
指令用於設定防火牆、封包處理、NAT 和其他封包處理作業。包裝函式可執行下列指令:
- 新增及刪除供應商鏈。
- 在任何鏈結中新增及刪除規則,這些鏈結會參照進入 (
-i
) 或離開 (-o
) 供應商介面的封包。
- 從任何鏈結中的任何位置跳至供應商鏈結。
ndc
ndc
用於與 netd
守護程進行通訊,後者會在 Android 上執行大部分的網路設定。包裝函式可允許下列指令:
- 建立及刪除 OEM 網路 (
oemXX
)。
- 將供應商管理的介面新增至原始設備製造商 (OEM) 網路。
- 新增路徑至 OEM 網路。
- 在全球和供應商介面上啟用或停用 IP 轉送。
tc
tc
指令可用於供應商介面上的流量排序和調整。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-09-03 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-03 (世界標準時間)。"],[],[],null,["The Android operating system contains standard Linux networking utilities\nsuch as `ifconfig`, `ip`, and `ip6tables`.\nThese utilities reside on the system image and enable configuration of the\nentire Linux networking stack. On devices running Android 7.x and lower,\nvendor code is allowed to call these binaries directly, which presents the\nfollowing problems:\n\n- Because network utilities are updated in the system image, they don't provide a stable implementation.\n- The scope of the networking utilities is so broad it is difficult to evolve the system image while guaranteeing predictable behaviour.\n\nOn devices running Android 8.0 and higher, the vendor partition remains the same\nwhile the system partition receives an update. To achieve this, Android 8.0\nprovides the ability to define a stable, versioned interface while also using\nSELinux restrictions to keep the interdependency of vendor and system image to a\nknown good set.\n\nVendors can use the platform-provided network configuration utilities to\nconfigure the Linux networking stack, but these utilities don't yet include a\nHIDL interface wrapper. To define such an interface, Android 8.0 includes the\n`netutils-wrapper-1.0` tool.\n\nNetutils wrapper\n\nThe `netutils` wrapper utility provides a subset of the Linux\nnetwork stack configuration that isn't affected by system partition updates.\nAndroid 8.0 contains version 1.0 of the wrappers, which allows you to pass the\nsame arguments as the wrapped utilities, installed in the system partition at\n`/system/bin` as follows: \n\n```actionscript-3\nu:object_r:system_file:s0 /system/bin/ip-wrapper-1.0 -\u003e netutils-wrapper-1.0\nu:object_r:system_file:s0 /system/bin/ip6tables-wrapper-1.0 -\u003e netutils-wrapper-1.0\nu:object_r:system_file:s0 /system/bin/iptables-wrapper-1.0 -\u003e netutils-wrapper-1.0\nu:object_r:system_file:s0 /system/bin/ndc-wrapper-1.0 -\u003e netutils-wrapper-1.0\nu:object_r:netutils_wrapper_exec:s0 /system/bin/netutils-wrapper-1.0\nu:object_r:system_file:s0 /system/bin/tc-wrapper-1.0 -\u003e netutils-wrapper-1.0\n```\n\nSymlinks show the networking utilities wrapped by the `netutils`\nwrapper, which include:\n\n- `ip`\n- `iptables`\n- `ip6tables`\n- `ndc`\n- `tc`\n\nTo use these utilities in Android 8.0 and higher, vendor implementations must\nadhere to the following rules:\n\n- Vendor processes must not execute `/system/bin/netutils-wrapper-1.0` directly; attempts to do so results in error.\n- All utilities wrapped by `netutils-wrapper-1.0` must be launched using their symlinks. For example, change the vendor code that did this before (`/system/bin/ip \u003cFOO\u003e \u003cBAR\u003e`) to `/system/bin/ip-wrapper-1.0 \u003cFOO\u003e \u003cBAR\u003e`.\n- Executing the wrappers without domain transition is prohibited in platform SELinux policy. This rule must not be changed and is tested against in the [Android Compatibility Test Suite (CTS)](/docs/compatibility/cts).\n- Executing the utilities directly (e.g., `/system/bin/ip \u003cFOO\u003e \u003cBAR\u003e`) from the vendor processes is also prohibited in the platform SELinux policies. This rule must not be changed and is tested against in CTS.\n- Any vendor domain (process) that needs to launch a wrapper must add the following domain transition rule in the SELinux policy: `domain_auto_trans(`\u003cvar translate=\"no\"\u003eVENDOR-DOMAIN-NAME\u003c/var\u003e`, netutils_wrapper_exec,\n netutils_wrapper)`.\n\n| **Note:** For details on SELinux in Android 8.0 and higher, see [Customizing\n| SEPolicy in Android 8.0+](/docs/security/features/selinux/customize#android-o).\n\nNetutils wrapper filters\n\nWrapped utilities can be used to configure almost any aspect of the Linux\nnetworking stack. However, to ensure it is possible to maintain a stable\ninterface and allow updates to the system partition, only certain combinations\nof command line arguments are allowed; other commands are rejected.\n\nVendor interfaces and chains\n\nThe wrapper has a concept of *vendor interfaces*. These are interfaces\ntypically managed by vendor code, such as cellular data interfaces. Typically,\nother types of interfaces (such as Wi-Fi) are managed by the HALs and the\nframework. The wrapper recognizes vendor interfaces by name (using a regular\nexpression) and allows vendor code to perform many operations on them.\nCurrently, vendor interfaces are:\n\n- Interfaces whose names end in \"oem\" followed by a number, such as `oem0` or `r_oem1234`.\n- Interfaces used by current SOC and OEM implementations, such as `rmnet_data[0-9]`.\n\nNames of interfaces that are typically managed by the framework (such as\n`wlan0`) are never vendor interfaces.\n\nThe wrapper has a similar concept of *vendor chains* . These are used\nin `iptables` commands and are also recognized by name. Currently,\nvendor chains:\n\n- Start with `oem_`.\n- Are used by current SOC and OEM implementations, e.g., chains starting in `nm_` or `qcom_`.\n\nAllowed commands\n\nCurrently allowed commands are listed below. Restrictions are implemented via\na set of regular expressions on the executed command lines. For details, refer\nto `system/netd/netutils_wrappers/NetUtilsWrapper-1.0.cpp`.\n\nip\n\nThe `ip` command is used to configure IP addresses, routing, IPsec\nencryption, and a number of other network parameters. The wrapper allows the\nfollowing commands:\n\n- Add and remove IP addresses from vendor-managed interfaces.\n- Configure IPsec encryption.\n\niptables and ip6tables\n\nThe `iptables` and `ip6tables` commands are used to\nconfigure firewalling, packet mangling, NAT, and other per-packet processing.\nThe wrapper allows the following commands:\n\n- Add and delete vendor chains.\n- Add and delete rules in any chain that refers to packets going into (`-i`) or out of (`-o`) a vendor interface.\n- Jump to a vendor chain from any point in any other chain.\n\nndc\n\n`ndc` is used to communicate to the `netd` daemon that\nperforms most network configuration on Android. The wrapper allows the following\ncommands:\n\n- Create and destroy OEM networks (`oemXX`).\n- Add vendor-managed interfaces to OEM networks.\n- Add routes to OEM networks.\n- Enable or disable IP forwarding globally and on vendor interfaces.\n\ntc\n\nThe `tc` command is used to configure traffic queueing and shaping\non vendor interfaces."]]