Debugging record of AP mode and network sharing Function for Android KitKat 4.4 Wifi Transplantation

Source: Internet
Author: User

Tethering technology has been widely used on mobile platforms. It can use mobile devices as an access point, and other devices can use Wi-Fi, connect to the mobile device using USB or Bluetooth. In Android, you can set Wi-Fi to AP mode as a WLAN Access Point to share Android Internet connection with other devices. After Android becomes an access point, it cannot connect to the Internet using Android applications through WLAN, but it can access the Internet through other methods, such as Ethernet or mobile network. In this case, Ethernet or mobile networks act as upstream in network sharing (Tethering.

Recently, the Wifi module has been debugged on the Atmel SAMA5D3-EK Development Board. Tethering needs to be implemented in Android, and the network connection is shared to other devices through the Wi-Fi AP mode.

On the Development Board, a wired network card (eth0), a wireless network card (wlan0), eth0 is connected to the Internet, and wlan0 is shared with other devices, such as Android phones, so that Android phones can connect to the Internet through the Development Board.

Hardware Platform: Atmel SAMA5

Software Platform: Linux 3.10 + Android 4.4

Wifi module: RTL8723AU (USB Interface)

Because the Kernel used is developed by the Vendor Based on the main kernel, although the basic Android support is added to the main kernel, it is not completely. During Android transplantation, if you find that some features on the Android upper layer lack Kernel support, you can apply the corresponding changes to the vendor's Linux Kernel Based on the Android kernel maintained by Google, this method is used in the previous article "Android KitKat 4.4 platform development-adding usb adb and MTP function support.

Throughout the Wifi porting process, Realtek, a Wifi module vendor, provided a detailed process. However, the content only covers the Wifi driver and Android. We have to decide which Linux kernel to use and how to configure the kernel.

Follow the transplant document provided by Realtek to port AndroidWifi. The following problems occur when testing the Wifi network sharing function:

In the "set" program "network sharing and portable Hotspot", the "portable Wi-Fi hotspot" is opened, and the Wifi hotspot is not actually enabled, but the corresponding single-region is continuously closed, so it is repeated.

For exceptions that occur during Android testing, there is usually an error message in the log. Although it is not absolutely accurate, you should analyze the log information before debugging, locate the code location that causes the exception.

After some analysis, it is estimated that the highlighted log information below may be the key code points that induce exceptions.

V/NatController (972): enableNat (intIface = , ExtIface = )

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 373 K, 54% free 6723 K/14460 K, paused 44 ms + 14 ms, 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


256 intNatController: setTetherCountingRules (booladd, 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, * proc_path;
263 intquota_fd;
264 asprintf (& quota_name, "% s _ % s", intIface, extIface );
265
266 asprintf (& proc_path, "/proc/net/xt_quota/% s", quota_name );
267 quota_fd = open (proc_path, O_RDONLY );
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 free (proc_path );
276
277 const char * 00002b [] = {
278 IPTABLES_PATH,
279 "-",
280 LOCAL_TETHER_COUNTERS_CHAIN,
281 "-I ",
282 intIface,
283 "-o ",
284 extIface,
285 "-m ",
286 "quota2 ",
287 "-- name ",
288 quota_name,
289 "-- grow ",
290 "-j ",
291 "RETURN"
292 };
293
294 if (runCmd (ARRAY_SIZE (00002b), 00002b) & add ){
295 free (quota_name );
296 return-1;
297}
298 free (quota_name );
299
300 asprintf (& quota_name, "% s _ % s", extIface, intIface );
301 asprintf (& proc_path, "/proc/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 free (proc_path );
311

Analyze the NatController: setTetherCountingRules and log information, and infer that the cause of the exception is to execute the command

/System/bin/iptables-A natctrl_tether_counters-I wlan0-o eth0-mquota2 -- name wlan0_eth0 -- grow-j failed.

The path/proc/net/xt_quota/is also involved, but this path does not exist in the current system. It is inferred that the kernel lacks support related to quota2 or xt_quota.

Find the possible cause of the problem, and then verify it. Comparing the differences between the Android Linux kernel, the vendor Linux kernel, and the main Linux kernel network, we found that the Android Linux kernel added quota2 support on the basis of the main Linux kernel. Involved in four submissions

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

Commit7d89969ad270d4c535e33830188806233bf35442

Author: Greg Hackmann

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

Netfilter: xt_qtaguid: 64-bit warning fixes

Change-Id: I2adc517c0c51050ed601992fa0ea4de8f1449414

Signed-off-by: Greg Hackmann

Net/netfilter/xt_quota2.c

Commit73570fe76d3b47e669558f06f1a18e0f02dff606

Author: Arve Hj? Nnev? G

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

Net/netfilter/xt_quota2.c

Commitea34f99edb73b67ef0a99d304887c64febd4c878

Author: JP Abgrall

Time: 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

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

-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,

-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

Net/netfilter/Kconfig

Net/netfilter/Makefile

Net/netfilter/xt_quota2.c

Commit3db08b39ea752741544e9c7733ce9ef54bed9f3b

Author: JP Abgrall

Time: 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 against SS 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

Include/linux/netfilter/xt_quota2.h

Net/netfilter/xt_quota2.c

Extract quota2-related commit and make Patches

$ Git format-patch-n4 net/netfilter/xt_quota2.c include/linux/netfilter/xt_quota2.hnet/netfilter/Kconfig net/netfilter/Makefile0001-netfilter-adding-the-original-quota2-from-xtables-ad.patch0002-netfilter-fixup-the-quota2-and-enable.patch0003-netfilter-xt_quota2-3.10-fixes.patch0004-netfilter-xt_qtaguid-64-bit-warning-fixes.patch

Apply these patches to the vendor's Linux kernel (git am command)

Added quota2 support in Kernel configuration

Commit4e6bf851ffd340f83062d053a6a20d358def121e

Author: Max Liao

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

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

Compile the kernel.

Test the Wifi network sharing function. The previous exception disappears and the function test is normal. This indicates that the previous estimation is correct. The exception is indeed caused by the lack of support for netfilter quota2 in the kernel.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.