Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 4.4 Wifi 移植之 AP 模式與網絡共享調試記錄

編輯:關於 Android 程式設計

Tethering(網絡共享)技術在移動平台上應用越來越廣泛,它把移動設備變成一個接入點,其他設備可通過 Wi-Fi、USB 或藍牙連接到它。在 Android 中,可以將 Wifi 設為 AP 模式作為 WLAN 接入點,與其他設備共享互聯網連接。此時,以太網或移動網絡在網絡共享中作為 upstream 的角色。

最近在 Atmel 的 SAMA5D3-EK 開發板上調試 Wifi 模塊,需要在 Android 下實現 Tethering,通過 Wi-Fi 的 AP 模式把網絡共享給其他設備。硬件配置為:一個有線網卡 eth0 連接外網,一個無線網卡 wlan0 作為 AP 共享給其他設備(如 Android 手機),使其能通過開發板訪問外網。

硬件平台:Atmel SAMA5;軟件平台:Linux 3.10 + Android 4.4;Wifi 模組:RTL8723AU(USB 接口)。

由於使用的內核是廠商基於主線內核改的,雖然主線內核加入了 Android 的基本支持,但並不完全。移植時若發現上層功能缺乏內核支持,可參考 Google 維護的 Android Linux 內核,把相應改動移植到廠商內核中。

問題現象

按照 Realtek 提供的文檔完成 Android Wifi 移植後,測試網絡共享時出現異常:在「設置」→「網絡共享與便攜式熱點」中打開「便攜式 Wi-Fi 熱點」,熱點並未真正開啟,而是單選框不斷地關閉又打開,反覆循環。

排查這類問題時,一般先分析 log。經過分析,以下日誌很可能是誘發異常的關鍵:

V/NatController(972): runCmd(/system/bin/iptables -A natctrl_tether_counters -i wlan0 -o eth0 -m quota2 --name wlan0_eth0 --grow -j RETURN) res=1 E/NatController(972): Error setting forward rules

可以看到,執行 iptables 的 quota2 規則時返回 res=1(失敗),並伴隨「Error setting forward rules」。同時涉及的 /proc/net/xt_quota/ 路徑在當前系統中並不存在,由此推斷內核缺少 quota2 / xt_quota 相關支持。

對照 NatController::setTetherCountingRules 函數(system/netd/NatController.cpp)可以確認:它會向 natctrl_tether_counters 鏈添加一條基於 quota2 的計量規則,若該命令失敗便返回 -1,最終導致前向上報錯、Tethering 無法建立。

定位根因

比對 Android Linux 內核、廠商內核與主線內核的網絡部分差異後發現:Android 內核在主線基礎上增加了 quota2 支持,涉及四次提交:

commit 3db08b3 netfilter: adding the original quota2 from xtables-addons commit ea34f99 netfilter: fixup the quota2, and enable. commit 73570fe netfilter: xt_quota2: 3.10 fixes. commit 7d89969 netfilter: xt_qtaguid: 64-bit warning fixes

內核中原有的 xt_quota 存在缺陷(按 per-CPU 計量、無法跨 IPv4/IPv6 統計),而 xt_quota2 正是為修復這些問題而引入。

解決方案

提取 quota2 相關提交製作補丁:

$ git format-patch -n4 net/netfilter/xt_quota2.c include/linux/netfilter/xt_quota2.h net/netfilter/Kconfig net/netfilter/Makefile 0001-netfilter-adding-the-original-quota2-from-xtables-ad.patch 0002-netfilter-fixup-the-quota2-and-enable.patch 0003-netfilter-xt_quota2-3.10-fixes.patch 0004-netfilter-xt_qtaguid-64-bit-warning-fixes.patch

用 git am 將補丁應用到廠商內核,並在內核配置中開啟 quota2 支持:

CONFIG_NETFILTER_XT_MATCH_QUOTA=y CONFIG_NETFILTER_XT_MATCH_QUOTA2=y

重新編譯內核並燒錄後,再次測試 Wifi 網絡共享功能,之前的異常現象消失,功能一切正常。這也印證了先前的推斷:異常的根因正是內核缺乏 netfilter quota2 支持。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved