我已经使用 NetworkManager/ModemManager 设置了我的 LTE 调制解调器,并且运行正常。我还使用 DHCP 在 NetworkManager 中将 Wifi 设置为接入点。
但是我如何通过 Wifi 共享 LTE 调制解调器互联网访问?我已启用 IP 转发 ( echo 1 > /proc/sys/net/ipv4/ip_forward
),还有什么?
# ifconfig
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:12946 errors:0 dropped:0 overruns:0 frame:0
TX packets:12946 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1297231 (1.2 MiB) TX bytes:1297231 (1.2 MiB)
wlan0 Link encap:Ethernet HWaddr E8:4F:25:DD:BD:51
inet addr:10.42.0.1 Bcast:10.42.0.255 Mask:255.255.255.0
inet6 addr: fe80::ea4f:25ff:fedd:bd51/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:77 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:12101 (11.8 KiB)
wwan0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
inet addr:100.72.254.176 P-t-P:100.72.254.176 Mask:255.255.255.224
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:1808 errors:0 dropped:0 overruns:0 frame:0
TX packets:2176 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:356293 (347.9 KiB) TX bytes:261384 (255.2 KiB)
# ip route
default via 100.72.254.177 dev wwan0 metric 700
10.42.0.0/24 dev wlan0 scope link src 10.42.0.1 metric 600
100.72.254.160/27 dev wwan0 scope link src 100.72.254.176 metric 700
Wifi 的 DHCP 由 NetworkManager 启动 dnsmasq 使用默认参数完成:
# ps -eF | grep dnsmasq
nobody 1104 606 0 1285 2444 0 09:05 ? 00:00:00 /usr/bin/dnsmasq --conf-file=/dev/null --no-hosts --keep-in-foreground --bind-interfaces --except-interface=lo --clear-on-reload --strict-order --listen-address=10.42.0.1 --dhcp-range=10.42.0.10,10.42.0.254,60m --dhcp-lease-max=50 --dhcp-leasefile=/var/lib/NetworkManager/dnsmasq-wlan0.leases --pid-file=/var/run/nm-dnsmasq-wlan0.pid --conf-dir=/etc/NetworkManager/dnsmasq-shared.d
参考NetworkManager 和 dnsmasq dhcp 地址范围)。
# cat /etc/NetworkManager/system-connections/ap.nmconnection
[connection]
id=ap
uuid=3205b229-8c4d-4766-8d63-bcd949d03321
type=wifi
autoconnect=false
interface-name=wlan0
[wifi]
band=bg
channel=1
mode=ap
ssid=MySSID
[wifi-security]
group=ccmp;
key-mgmt=wpa-psk
pairwise=ccmp;
proto=rsn;
psk=MyPSK
[ipv4]
method=shared
[ipv6]
addr-gen-mode=stable-privacy
method=ignore
[proxy]
一位同事建议使用https://github.com/oblique/create_ap。我尝试了 NAT 和桥接选项,但都失败了。
网络地址转换(NAT):
# create_ap -w 2 wlan0 wwan0 APTest 12345678
WARN: brmfmac driver doesn't work properly with virtual interfaces and
it can cause kernel panic. For this reason we disallow virtual
interfaces for your adapter.
For more info: https://github.com/oblique/create_ap/issues/203
WARN: Your adapter does not fully support AP virtual interface, enabling --no-virt
Config dir: /tmp/create_ap.wlan0.conf.XX59Vdf8
PID: 537195
Network Manager found, set wlan0 as unmanaged device... [46988.754925] ieee80211 phy1: brcmf_set_pmk: failed to change PSK in firmware (len=0)
[46988.784848] ieee80211 phy1: brcmf_vif_set_mgmt_ie: vndr ie set error : -52
[46988.792027] ieee80211 phy1: brcmf_vif_set_mgmt_ie: vndr ie set error : -52
DONE
Sharing Internet using method: nat
iptables v1.8.7 (legacy): unknown option "--to-ports"
Try `iptables -h' or 'iptables --help' for more information.
Doing cleanup.. done
桥:
# create_ap -w 2 -m bridge wlan0 wwan0 APTest 12345678
WARN: brmfmac driver doesn't work properly with virtual interfaces and
it can cause kernel panic. For this reason we disallow virtual
interfaces for your adapter.
For more info: https://github.com/oblique/create_ap/issues/203
WARN: Your adapter does not fully support AP virtual interface, enabling --no-virt
Config dir: /tmp/create_ap.wlan0.conf.XXgGPNON
PID: 540585
Network Manager found, set wlan0 as unmanaged device... DONE
Sharing Internet using method: bridge
Create a bridge interface... ip: RTNETLINK answers: Operation not supported
对于 IPv4,您需要的部分包括:
什么是 NAT 以及为什么需要它?
互联网上的所有 IP 地址均在IANA上注册(以块为单位) 。互联网本身会为每个注册的 IP 地址(块)组织路由,因此 IP 地址本身便会告诉每个路由器将数据包转发到何处。
但是您的本地网络 IP 地址(您的情况是 10.42.0.x)未在 IANA 注册,互联网路由器不知道如何找到您。无论如何,您都不拥有该地址,但它被保留给任何人使用。
网络地址转换将传出数据包上的本地 IP 地址与路由器自己的 [公共] IP 地址进行交换,以便互联网上的服务器看到路由器的公共 IP 地址,而不是设备的本地网络 IP 地址。然后,路由器对传入数据包进行反向交换,然后再将它们发送回本地设备。
使用 iptables 设置 NAT
不要忘记您可能需要
sudo
并且不要忘记 iptables 通常不会在重启时保存。有关如何在 Ubuntu 中保留 iptables 规则的说明可在此处找到:https://askubuntu.com/questions/84781/iptables-resets-when-server-rebootsiptables 规则:
按照https://community.unix.com/t/iptables-v1-8-7-nf-tables-unknown-option-to-ports/385377/7,我将其添加
strace
到我的设备:看起来我们缺少了
/usr/lib/xtables/libipt_owner.so
。https ://forums.gentoo.org/viewtopic-t-754259-start-0.html表明这需要CONFIG_NETFILTER_XT_MATCH_OWNER=m
所以我尝试了:有趣的是,在工作命令上运行时,
strace
发生变化的行是getsockopt
从:到:
NAT 仍然失败,因此再次尝试 {{strace}}:
之前我们在 时失败了
IPT_SO_GET_REVISION_MATCH
,现在 是IPT_SO_GET_REVISION_TARGET
。我在 Linux v5.4.238 源代码中查找了它的用法: https://elixir.bootlin.com/linux/v5.4.238/C/ident/IPT_SO_GET_REVISION_TARGET 它们用于相同的函数。我还注意到,如果你在没有 的情况下运行,--to-ports
你会得到:So if adding
CONFIG_NETFILTER_XT_MATCH_OWNER
fixedIPT_SO_GET_REVISION_MATCH
, would addingCONFIG_NETFILTER_XT_TARGET_REDIRECT
fixIPT_SO_GET_REVISION_TARGET
?Yes it does, NAT method now works.