Android KitKat 4.4 Wifi移植AP模式和网络共享的调试日志

Android KitKat 4.4 Wifi移植AP模式和网络共享的调试日志

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

Tethering技术在移动平台上已经运用的越来越广泛了。它能够把移动设备当做一个接入点,其它的设备能够通过Wi-Fi。USB或是Bluetooth等方式连接到此移动设备。在Android中能够将Wifi设为AP模式作为WLAN接入点。从而与其它设备共享Android的互联网连接。Android成为接入点后。就无法通过WLAN连接使用Android的应用程序訪问互联网,但能够通过其它方式如以太网或移动网络訪问互联网。

此时以太网或移动网络在网络共享(Tethering)中是作为upstream的角色。

 

近期在AtmelSAMA5D3-EK开发板上调试Wifi模块。须要在Android下实现Tethering,通过Wi-Fi的AP模式。将网络连接共享给其他设备。

 

开发板上一个有线网卡(eth0),一个无线网卡(wlan0)。eth0连接到外网,wlan0作为AP共享给其它设备比方Android手机,使得Android手机能够通过开发板连接到外网。

 

硬件平台:Atmel SAMA5

软件平台:Linux 3.10 +Android 4.4

Wifi模组:RTL8723AU(USB接口)

 

由于使用的内核是厂商基于主线内核开发的,尽管主线内核中加入了Android的基本支持,但并不全然。

在做Android移植时,假设发现Android上层的某些功能缺乏内核的支持,能够依据Google维护的AndroidLinux内核将对应的更改应用到厂商Linux内核中,在前文《Android KitKat 4.4平台开发-加入USBADB和MTP功能支持》中就是使用的这样的方法。

 

整个Wifi移植过程,Wifi模组厂商Realtek给出了具体的过程。但内容仅仅涉及Wifi驱动及Android部分。使用哪个Linux内核以及内核怎样配置须要我们自己决定。

 

依照Realtek提供的移植文档进行AndroidWifi的移植,在測试Wifi 网络共享功能时出现例如以下问题:

在“设置”程序“网络共享与便携式热点”中,打开“便携式Wi-Fi热点”。并没有真正开启Wifi热点。而是对应单选框不断关开,如此重复。

 

对于在測试Android功能时出现的异常情况。一般在log中会存在对应的错误信息,尽管并非绝对准确,但调试时应该考虑先分析log信息,尝试从中定位导致异常发生的代码位置。

经过一番分析猜測。例如以下高亮显示的log信息非常有可能是诱发异常发生的关键代码点。

V/NatController(972): enableNat(intIface=<wlan0>, extIface=<eth0>)

V/NatController(972): runCmd(/system/bin/iptables -t nat -A natctrl_nat_POSTROUTING -o eth0 -jMASQUERADE) res=0

V/NatController(972): runCmd(/system/bin/iptables -A natctrl_FORWARD -i eth0 -o wlan0 -m state–state ESTABLISHED,RELATED -g natctrl_tether_counters) res=0

D/dalvikvm(1339): GC_CONCURRENT freed 373K, 54% free 6723K/14460K, paused 44ms+14ms, total219ms

V/NatController(972): runCmd(/system/bin/iptables -A natctrl_FORWARD -i wlan0 -o eth0 -m state–state INVALID -j DROP) res=0

V/NatController(972): runCmd(/system/bin/iptables -A natctrl_FORWARD -i wlan0 -o eth0 -gnatctrl_tether_counters) res=0

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

V/NatController(972): runCmd(/system/bin/iptables -D natctrl_FORWARD -i wlan0 -o eth0 -m state–state INVALID -j DROP) res=0

V/NatController(972): runCmd(/system/bin/iptables -D natctrl_FORWARD -i eth0 -o wlan0 -m state–state ESTABLISHED,RELATED -g natctrl_tether_counters) res=0

E/NatController( 972): Error setting forward rules

V/NatController(972): runCmd(/system/bin/iptables -F natctrl_FORWARD) res=0

V/NatController(972): runCmd(/system/bin/iptables -A natctrl_FORWARD -j DROP) res=0

http://androidxref.com/4.4.2_r1/xref/system/netd/NatController.cpp#294

256int NatController:: add, const char *intIface, const char *extIface) {
257
258    /* We only ever add tethering quota rules so thatthey stick. */
259    if (!add) {
260        return 0;
261    }
262    char *quota_name, *263    int 264    quota_name, “%s_%s”, 265
266    asprintf(&proc_path, “/proc/net/xt_quota/%s”, quota_name);
267    quota_fd = proc_path, 268    if (quota_fd >= 0) {
269        /* quota for iface pair already exists*/
270        free(proc_path);
271        free(quota_name);
272        return 0;
273    }
274    close(quota_fd);
275    proc_path);
276
277    const char *278            IPTABLES_PATH,
279            “-A”,
280            LOCAL_TETHER_COUNTERS_CHAIN,
281            “-i”,
282            intIface,
283            “-o”,
284            285            “-m”,
286            “quota2”,
287            “–name”,
288            quota_name,
289            “–grow”,
290            “-j”,
291          “RETURN”
292    };
293
294    if (runCmd(ARRAY_SIZE(cmd2b), cmd2b) && add) {
295        free(quota_name);
296        return –1;
297    }
298    free(quota_name);
299
300    quota_name, “%s_%s”, extIface, intIface);
301    proc_path, “//net/xt_quota/%s”, quota_name);
302    quota_fd = open(proc_path, O_RDONLY);
303    if (quota_fd >= 0) {
304        /* quota for iface pair already exists*/
305        free(proc_path);
306        free(quota_name);
307        return 0;
308    }
309    close(quota_fd);
310    proc_path);
311

 

分析这个函数NatController::setTetherCountingRules及log信息,判断出异常发生的原因是运行命令

/system/bin/iptables -A natctrl_tether_counters -i wlan0 -o eth0 -mquota2 –name wlan0_eth0 –grow -j 失败。

并且还涉及到路径/proc/net/xt_quota/,但当前系统下并不存在这个路径。

由此判断应该是内核缺乏与quota2或xt_quota相关的支持。

 

找到问题的可能原因,接下来就是验证了。比較Android Linux内核、厂商Linux内核以及主线Linux内核网络部分的差异。发现Android Linux内核在主线Linux内核基础上添加了quota2的支持。

涉及四次提交

$git log –name-only net/netfilter/xt_quota2.cinclude/linux/netfilter/xt_quota2.h net/netfilter/Kconfignet/netfilter/Makefile

commit7d89969ad270d4c535e33830188806233bf35442

Author:Greg Hackmann <ghackmann@google.com>

Date:Mon Feb 24 09:39:46 2014 -0800

    netfilter: xt_qtaguid: 64-bit warning fixes

    Change-Id:I2adc517c0c51050ed601992fa0ea4de8f1449414

    Signed-off-by: Greg Hackmann<ghackmann@google.com>

net/netfilter/xt_quota2.c

 

commit73570fe76d3b47e669558f06f1a18e0f02dff606

Author:Arve Hjønnevåg <arve@android.com>

Date:Mon May 13 20:42:46 2013 -0700

    netfilter: xt_quota2: 3.10 fixes.

    – Stop using obsolete create_proc_entry api.

    – Use proc_set_user instead of directlyaccessing the private structure.

    Signed-off-by: Arve Hjønnevåg<arve@android.com>

net/netfilter/xt_quota2.c

 

commitea34f99edb73b67ef0a99d304887c64febd4c878

Author:JP Abgrall <jpa@google.com>

Date:Tue Jul 12 12:02:59 2011 -0700

    netfilter: fixup the quota2, and enable.

    The xt_quota2 came from

      http://sourceforge.net/projects/xtables-addons/develop

    It needed tweaking for it to compile withinthe kernel tree.

    Fixed kmalloc() and create_proc_entry()invocations within

     a non-interruptible context.

    Removed useless copying of current quotaback to the iptable’s

    struct matchinfo:

      – those are per CPU: they will changerandomly based on which

        cpu gets to update the value.

      – they prevent matching a rule: e.g.

          -A chain -m quota2 –name q1 –quota 123

         can’t be followed by

          -D chain -m quota2 –name q1 –quota 123

        as the 123 will be compared to the structmatchinfo’s quota member.

    Use the NETLINK NETLINK_NFLOG family to loga single message

    when the quota limit is reached.

    It uses the same packet type as ipt_ULOG,but

     – never copies skb data,

     – uses 112 as the event number (ULOG’s +1)

    It doesn’t log if the module param”event_num” is 0.

    Change-Id:I021d3b743db3b22158cc49acb5c94d905b501492

    Signed-off-by: JP Abgrall<jpa@google.com>

net/netfilter/Kconfig

net/netfilter/Makefile

net/netfilter/xt_quota2.c

 

commit3db08b39ea752748744e9c7733ce9ef54bed9f3b

Author:JP Abgrall <jpa@google.com>

Date:Tue Jun 21 11:14:49 2011 -0700

    netfilter: adding the original quota2 fromxtables-addons

    The original xt_quota in the kernel is plainbroken:

      – counts quota at a per CPU level

        (was written back when ubiquitous SMP wasjust a dream)

      – provides no way to count across IPV4/IPV6.

    This patch is the original unaltered codefrom:

      http://sourceforge.net/projects/xtables-addons

      at commite84391ce665cef046967f796dd91026851d6bbf3

    Change-Id:I19d49858840effee9ecf6cff03c23b45a97efdeb

    Signed-off-by: JP Abgrall<jpa@google.com>

include/linux/netfilter/xt_quota2.h

net/netfilter/xt_quota2.c

 

提取quota2相关的commit,制作补丁

$ git format-patch -n4 net/netfilter/xt_quota2.c include/linux/netfilter/xt_quota2.hnet/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
 

将这些补丁应用到厂商Linux内核 git am命令)

 

在内核配置中添加quota2支持

commit4e6bf851ffd340f83062d053a6a20d358def121e

Author:Max Liao <liaops@embedinfo.com>

Date:Mon Jun 16 06:08:25 2014 -0400

    ARM: at91/SAMA5: android_ubifs_defconfig:add netfilter quota2 support for sama5d3 and sama5d4

    Signed-off-by: Max Liao<liaops@embedinfo.com>

diff–git a/arch/arm/configs/sama5_android_ubifs_defconfigb/arch/arm/configs/sama5_android_ubifs_defconfig

index9881f7d..48c68a1 100644

—a/arch/arm/configs/sama5_android_ubifs_defconfig

+++b/arch/arm/configs/sama5_android_ubifs_defconfig

@@-623,6 +623,7 @@ CONFIG_NETFILTER_XT_MATCH_OSF=y

 CONFIG_NETFILTER_XT_MATCH_OWNER=y

 CONFIG_NETFILTER_XT_MATCH_PKTTYPE=y

 CONFIG_NETFILTER_XT_MATCH_QUOTA=y

+CONFIG_NETFILTER_XT_MATCH_QUOTA2=y

 CONFIG_NETFILTER_XT_MATCH_RATEEST=y

 CONFIG_NETFILTER_XT_MATCH_REALM=y

 CONFIG_NETFILTER_XT_MATCH_RECENT=y

 

编译内核。

 

測试Wifi 络共享功能,之前的异常现象消失,功能測试正常,这说明之前的推导推測是正确的。异常的原因的确是内核缺乏netfilter quota2支持

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117726.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 关于DialogResult的用法

    关于DialogResult的用法在程序中,经常会弹出一个对话框来让用户填写一些信息,填写完成后,当用户点击“确定”按钮后,在主窗体中进行其他的处理。比如一个简单的例子,在主窗体中有一个菜单,是“增加用户”,当点击这个菜单之后,我们需要弹出一个增加用户的窗体出来,就假设“增加用户”的窗体叫frmAddUser,那么代码如下:1:frmAddUserfrm=newfrmAddUser();2: 3:i…

    2022年6月22日
    29
  • linux c++进程间通信_c++多线程通信

    linux c++进程间通信_c++多线程通信Linux下c开发之——线程间通信2016-02-1817:50

    2022年9月2日
    2
  • 聚集索引和非聚集索引的区别[通俗易懂]

    聚集索引和非聚集索引的区别[通俗易懂]一、深入浅出理解索引结构实际上,可以把索引理解为一种特殊的目录。微软的SQLSERVER提供了两种索引:聚集索引(clusteredindex,也称聚类索引、簇集索引)和非聚集索引(nonclusteredindex,也称非聚类索引、非簇集索引)。下面,我们举例来说明一下聚集索引和非聚集索引的区别:其实,我们的汉语字典的正文本身就是一个聚集索引。比如,我们要查“安”字,因为“安”的拼音是…

    2022年5月15日
    36
  • Centos系统下Lamp环境的快速搭建(超详细)

    Centos系统下Lamp环境的快速搭建(超详细)

    2021年9月22日
    61
  • MD编辑器设置字体、颜色、大小

    MD编辑器设置字体、颜色、大小颜色:浅红色文字:浅红色文字:深红色文字:深红色文字浅绿色文字:浅绿色文字深绿色文字:深绿色文字浅蓝色文字:浅蓝色文字深蓝色文字:深蓝色文字浅黄色文字:浅黄色文字深黄色文字:深黄色文字浅青色文字:浅青色文字深青色文字:深青色文字浅紫色文字:浅紫色文字深紫色文字:深紫色文字大小:size为1:size为1size为2:size为2size为3:size为3size为4:size为4size为10:size为10字体:我是黑体字我是宋体字我是微软雅黑字我是fanta

    2022年9月15日
    0
  • python 安装第三方模块

    python 安装第三方模块

    2021年7月1日
    77

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号