Android dual network card configuration provides external network access for PCs connected to the Android host (1)

Yesterday I completed the debugging of the Linux dual network card configuration to provide external network access for PCs connected to the Linux host. Today I want to test whether the Android system supports it. I thought I could just use yesterday's script and found that there are still some differences. This article is generally similar to the Linux debugging process. This article simply verifies the feasibility of the Android system and verifies some steps in detail and completes an Android script. I have verified the command sequence and various status commands at least 10 times and it is quite good. Troublesome.

Considering the complexity of the Android system, I plan to add this function to the system interface when I have time to configure the internal and external network configuration of dual network cards after the code call is completed.

Android dual network card configuration provides external network access for PCs connected to the Android host

In this article, I will introduce how to use the RK3568 Android11 ​​motherboard to provide external network access to a PC connected to its internal network port. I will use an actual troubleshooting case to explain this process in detail, and finally implement various checks and configurations using scripts.

background

The customer has a RK3568 Android11 ​​motherboard, which has two Ethernet interfaces: eth0 (internal network) and eth1 (external network). The customer wants to provide external network access via RK3568 to a Windows PC connected to eth0.

In short, the Windows PC is connected to the internal network port of the RK3568 motherboard through a network cable, and then www.baidu.com (external network) can be accessed from the Windows PC.

Windows PC settings

The PC is Windows 10 and the default gateway needs to be set through the graphical interface or command line:

  1. Open the Control Panel.
  2. Select Network and Sharing Center.
  3. On the left, click "Change adapter settings."
  4. Right-click on the network connection (probably "Ethernet" or something similar) and select "Properties."
  5. Find and double-click "Internet Protocol Version 4 (TCP/IPv4)" in the list.
  6. Select "Use the following IP address" and enter your IP address and subnet mask. In Default Gateway, enter 192.168.52.2.
  7. Click "OK" to save the settings.
    Insert image description here
#执行ipconfig简单确认下
ipconfig

![Windows PC Network Settings]

RK3568 Android11 ​​settings

Configure NAT (Network Address Translation)

Make sure you have the correct NAT rules set up on the RK3568.

iptables -F
iptables -t nat -F
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Configure a dual network port environment

If there is not an interface on the same subnet as the gateway you are trying to add, you may need to configure an interface to use this subnet. For example, if you have an eth0interface named , you can assign it an IP address using the following command:

ifconfig eth0 192.168.52.2 netmask 255.255.255.0 up

192.168.52.2is an example IP address, you can choose any available address on that subnet.

configure routing

In order for RK3568 to forward traffic correctly, the routing table needs to be configured. Routes can be added using the following command:

ip route add default via 192.168.1.1 dev eth1 metric 102
ip route add 192.168.1.0/24 dev eth1 metric 102
ip route add 192.168.52.0/24 dev eth0

Here 192.168.1.1is the default gateway of the external network interface eth1, 192.168.1.0/24the subnet where the external network interface eth1 is located, 192.168.52.0/24and the subnet where the internal network interface eth0 is located.

Troubleshooting steps

If your Windows 10 PC cannot access the external network through RK3568, you need to perform a series of troubleshooting steps.

Ping test :

  • Ping the RK3568’s internal IP address (192.168.52.2) from your PC. Make sure it's open.
  • Ping the PC's IP address (192.168.52.1) from the RK3568. Make sure it's passable too.
  • Ping external website from RK3568, such as 8.8.8.8. Make sure the external network is accessible.
  • Ping 8.8.8.8 from PC. If this doesn't work, but all of the above steps do, the problem may be with NAT or routing.

Check NAT settings : Make sure NAT is set up correctly on the RK3568. Run the following command again:

iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Check DNS : The problem may be related to DNS resolution. Try pinging a domain name from your PC, eg www.baidu.com. If this doesn't work, but pinging 8.8.8.8 does, you may need to set up a static DNS on your PC, such as Google's 8.8.8.8 and 8.8.4.4.

Firewall settings : Make sure the RK3568’s firewall allows traffic to pass. Run the following command:

iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

Script

In order to easily configure and check the network environment, you can use the following script to automatically execute the above steps:
execute it once and if it fails, execute it again, boot into the launcher and then execute it again, otherwise the network will not be up.

#!/system/bin/sh

# 检查参数数量
if [ "$#" -ne 3 ]; then
    echo "使用方法: $0 <内网接口名> <内网接口IP地址> <外网接口名>"
    exit 1
fi

# 获取参数
INTERNAL_IF="$1"
IPADDR="$2"
EXTERNAL_IF="$3"

# 启用IP转发
echo "启用IP转发..."
echo 1 > /proc/sys/net/ipv4/ip_forward

# 清除现有的iptables规则
echo "清除现有的iptables规则..."
iptables -F
iptables -t nat -F

# 设置NAT规则
echo "设置NAT规则..."
iptables -t nat -A POSTROUTING -o $EXTERNAL_IF -j MASQUERADE

# 配置内网接口
echo "配置内网接口 $INTERNAL_IF..."
ifconfig $INTERNAL_IF $IPADDR netmask 255.255.255.0 up

# 等待一段时间,确保内网接口已经启动并配置
sleep 5

# 删除可能存在的默认路由
ip route del default 2>/dev/null

# 配置路由
echo "配置路由..."
ip route add default via 192.168.1.1 dev $EXTERNAL_IF metric 102
ip route add 192.168.1.0/24 dev $EXTERNAL_IF metric 102
ip route add 192.168.52.0/24 dev $INTERNAL_IF

# 检查配置是否正确
ETH0_IP=$(ifconfig $INTERNAL_IF | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')
ETH1_IP=$(ifconfig $EXTERNAL_IF | grep 'inet addr' | cut -d: -f2 | awk '{print $1}')

if [ "$ETH0_IP" != "$IPADDR" ] || [ "$ETH1_IP" != "192.168.1.10" ]; then
    # 如果配置不正确,可以在此处添加其他命令或者发送通知
    echo "网络配置错误!"
fi

# 测试外网连接
echo "测试外网连接..."
ping -c 4 8.8.8.8

echo "脚本执行完毕!"

You can now run the script using the following command:

./enable_nat_routing.sh eth0 192.168.52.2 eth1

Script execution results:

rk3568_r:/vendor # ./setup_network.sh
RTNETLINK answers: No such process
rk3568_r:/vendor # busybox route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.1     0.0.0.0         UG    102    0        0 eth1
192.168.1.0     *               255.255.255.0   U     102    0        0 eth1
192.168.52.0    *               255.255.255.0   U     0      0        0 eth0
rk3568_r:/vendor # ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 66:64:66:98:2b:a4  Driver rk_gmac-dwmac
          inet addr:192.168.52.2  Bcast:192.168.52.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1249 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1496 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:178245 TX bytes:270750
          Interrupt:38

rk3568_r:/vendor # ifconfig eth1
eth1      Link encap:Ethernet  HWaddr 0a:0c:11:22:33:c7  Driver rk_gmac-dwmac
          inet addr:192.168.1.10  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::5103:8c9e:a84e:17f8/64 Scope: Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2576 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1588 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:412726 TX bytes:230968
          Interrupt:42

To be solved and questions:

  1. RK 3568 Android cannot ping PC?
  2. PC cannot ping RK3568?
  3. Implement the Android system interface to complete the configuration of dual network card Internet access rules.

I will add more to the above pitfalls when I have time.

Summarize:

Providing network access to devices connected to an Android host may require multiple configuration steps. By carefully examining each configuration and performing step-by-step troubleshooting, we can successfully resolve these types of issues.

Guess you like

Origin blog.csdn.net/SHH_1064994894/article/details/132556573