Configure the 4G module to access the Internet through the WAN port

Configure the 4G module to access the Internet through the WAN port

Configure the Linux board as a switch or router to provide the Internet access function similar to a router. Here, the 4G network card is used as the WAN to access the Internet.

1. Use a bridge to access the Internet

1. Configure the 4G module as the default gateway

First configure the 4G module as the default routing gateway.

Here you need to configure the 4G module in routing mode first. The difference between the 4G module routing mode and the network card mode is that after the dial-up is successful, the IP address allocated to the usb0 network card in the routing mode is the LAN IP (192.168.xx), while the IP allocated by the network card mode is the public network IP address (10.21.26.197) .

Disconnect the wired and wireless network connection of the board, and use it ping baidu.comfor testing. If you can access the Internet, it means that the network configuration of the 4G module is normal. Otherwise, you need to ip routeconfigure the 4G module as the default gateway, and ensure that the 4G module has dialed up and can access the Internet normally.

2. Create a bridge

Reference: https://blog.csdn.net/duapple/article/details/127001192?spm=1001.2014.3001.5501

Bridge the network card of the 4G module usb0with other network ports. I have two Ethernet ports on the board here. The experiment only uses one network port, so only one is bridged to connect to the computer for testing.

brctl addbr br0
brctl addif br0 usb0
brctl addif br0 eth0
ifconfig eth0 0.0.0.0
ifconfig usb0 0.0.0.0
ifconfig br0 up

Then you need to configure the IP of br0 to be in the same network segment as the 4G module. In the first step, there will be a LAN IP on usb0 when going online: 192.168.43.100. Here the IP of br0 can be configured as this:

ifconfig br0 192.168.43.100/24

Then set the br0 network card as the default route:

ip route add default via 192.168.43.1 dev br0

Then you should be able to ping the 4G module gateway 192.168.43.1, and then you should be able to ping baidu.com.

3. PC configuration

Here, the computer is connected to the Linux board through Ethernet, the PC Ethernet address is configured as a network segment with br0, and the gateway address is configured as the gateway address of the 4G module 192.168.43.1. DNS address configuration 114.114.114.114. Pay attention to disable other network adapters that can access the Internet, such as wireless network cards, so as not to affect the test.

image-20221204215541670

You may not be able to access the Internet at this time. First ping the br0 network card address of the Linux board on Windows 192.168.43.100, and then ping the gateway address after the connection is established 192.168.43.1. Then ping baidu.com again. After that, the whole network will be connected. The reason for the network failure at the beginning may be that the address learning has only started, which leads to network instability, and repeated pings to baidu.com network are intermittent, and even packet loss. Once it's stabilized, it's fine.

image-20221204220057058 image-20221204220142794 The network speed test on Windows is similar to the test results on the Linux board, 4G is still too slow.

4. Summary

At this time, as long as you use a network cable to connect to the Linux board, and manually configure the IP, you can use the 4G module on the Linux board to go online.

2. Use NAT to surf the Internet

Reference: https://codeantenna.com/a/4ZBrXkM9Zy

You need to use iptables to configure NAT and complete forwarding.

1. Configure iptables/netfilter

NAT is netfiltercompleted by the kernel module, iptableswhich is a tool in user space, and the two need to be used together.

1.1 netfilter

First configure in the kernel netfilter.

image-20221206215159617 image-20221206215159617 image-20221206215159617 image-20221206215159617

Note that it needs to be turned on: <*> IPv4 connection tracking support (required for NAT)to see -*- IPv4 NATand <*> iptables NAT support.

After the configuration is complete, recompile and burn the kernel.

1.2 iptables

This file system usually comes with it. If not, use the cross-compilation toolchain to compile it yourself, and then put it on the development board. In order to avoid the introduction of other libraries libmnl, add parameters to the cross-compilation configuration --disable-nftables.

To configure address forwarding, first enable forwarding:

echo "1" > /proc/sys/net/ipv4/ip_forward

Then configure:

iptables -F
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -t nat -A POSTROUTING -o usb0 -j MASQUERADE

Just do not report an error.

2. Configure 4G module

It is still necessary to ensure that the 4G module can access the Internet normally.

Here configure the 4G module as network card mode. Take quectelthe 4G module as an example, the AT command is AT+QCFG="NAT",1, after the configuration is completed, it needs to be restarted to take effect. Restart, after the dial-up is completed, the 4G module will be assigned an IP address similar to the public network IP (the address at the beginning of the 10th segment).

Then usb0configure the network card of the 4G module as the default route. Generally, after the above dial-up is completed, the system will automatically configure the routing table.

root@ATK-IMX6U:~# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.40.163.52    0.0.0.0         UG    0      0        0 usb0
10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 usb0
10.40.163.52    0.0.0.0         255.255.255.255 UH    0      0        0 usb0
183.230.126.224 10.40.163.52    255.255.255.255 UGH   0      0        0 usb0
183.230.126.225 10.40.163.52    255.255.255.255 UGH   0      0        0 usb0
192.168.10.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0

My system is automatically configured with multiple rules.

Then ping baidu.comthere is no problem, then it is considered a success.

root@ATK-IMX6U:~# ping baidu.com
PING baidu.com (110.242.68.66) 56(84) bytes of data.
64 bytes from 110.242.68.66: icmp_seq=1 ttl=48 time=59.1 ms
64 bytes from 110.242.68.66: icmp_seq=2 ttl=48 time=57.4 ms
64 bytes from 110.242.68.66: icmp_seq=3 ttl=48 time=55.5 ms
64 bytes from 110.242.68.66: icmp_seq=4 ttl=48 time=67.4 ms
^C
--- baidu.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 55.571/59.903/67.421/4.522 ms

If the ping is different from baidu, then modify /etc/resolv.confand add the following content:

nameserver 114.114.114.114
nameserver 127.0.0.1

Then ping should be no problem.

3. Network configuration

In addition to the network configuration of the 4G module, it is also necessary to configure the client and the network port connected to the client.

3.1 Linux network port configuration

The network port (eth0) connecting the Linux board to the client needs to be configured with a gateway address, eg 192.168.10.1. Otherwise, the client cannot access the Internet.

3.2 Client network configuration

Here the client uses PC Windows. The IP connected to the client computer and the Linux board should be set in the same network segment, for example 192.168.10.200, the gateway address should be set to 192.168.10.1. At this time, the Internet is already available, and the qq network is available.

image-20221206215159617

But ping baidu is unsuccessful, and the browser access to baidu is also unsuccessful. Here, set the DNS address to 114.114.114.114. Then ping baidu again, and found that it is already available, and there is no problem with browser access.

C:\Users\28328>ping baidu.com

正在 Ping baidu.com [110.242.68.66] 具有 32 字节的数据:
来自 110.242.68.66 的回复: 字节=32 时间=58ms TTL=47
来自 110.242.68.66 的回复: 字节=32 时间=79ms TTL=47
来自 110.242.68.66 的回复: 字节=32 时间=73ms TTL=47
来自 110.242.68.66 的回复: 字节=32 时间=55ms TTL=47

110.242.68.66 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 55ms,最长 = 79ms,平均 = 66ms

Note: the same, you need to disable other network cards that can access the external network during the test to prevent the test from being affected.

Guess you like

Origin blog.csdn.net/duapple/article/details/128178394