garywill / linux-router

Set Linux as router in one command. Support Internet sharing, redsocks, Wifi hotspot, IPv6. Can also be used for routing VM/containers 🛰️ (也欢迎关注B站 https://space.bilibili.com/2123686105 )
GNU Lesser General Public License v2.1
1.67k stars 148 forks source link

5G Support #61

Closed kphanipavan closed 9 months ago

kphanipavan commented 9 months ago

Addresses #51 and below

Fixes:

  1. Adds Hotspot 2.0 support, which can be enabled using --hs20 flag, subject to the user's hostapd build.
  2. Adds forcing the hotspot to specified channel using --force_channel flag. Some WiFi cards, like my Intel AX201, can transmit to a channel while being connected to a WiFi AP in different channel.
  3. Format the flag options in the help section into relevant blocks, namely WiFi 4 and WiFi 5 config.
  4. Adds --use_ht and --use_vht separately to add require_ht and require_vht config option to hostapd config. They are mandatory if --ht_capab or --vht_capab are used.

Tests:

1. Using the stock config:

Command:

sudo ./lnxrouter -o wlp0s20f3 -g 11 --dns 127.0.0.1 --hostname - --ap wlp0s20f3 "Plant Hotspot" -p helios123 -c 149 --country IN --ieee80211n --ieee80211ac --ht_capab "[HT40+][HT40-][SHORT-GI-40][SHORT-GI-20][RX-STBC1][DSSS_CCK-40]" --vht_capab "[MAX-MPDU-11454][VHT160][RXLDPC][SHORT-GI-80][SHORT-GI-160][TX-STBC-2BY1][SU-BEAMFORMEE][MU-BEAMFORMEE]" --virt-name ap0

Fails to turn on hotspot as the WiFi card is already connected to my Univ's 5G WiFi, which is on channel 56 and which is not supported, as seen below:

Screenshot: ![Screenshot_20230928_122253](https://github.com/garywill/linux-router/assets/60005847/81bf7e26-f8ac-491d-a8f9-67b828e19c43)

2. Using --force_channel flag to overcome above:

Command:

sudo ./lnxrouter -o wlp0s20f3 -g 11 --dns 127.0.0.1 --hostname - --ap wlp0s20f3 "Plant Hotspot" -p helios123 -c 149 --country IN --ieee80211n --ieee80211ac --ht_capab "[HT40+][HT40-][SHORT-GI-40][SHORT-GI-20][RX-STBC1][DSSS_CCK-40]" --vht_capab "[MAX-MPDU-11454][VHT160][RXLDPC][SHORT-GI-80][SHORT-GI-160][TX-STBC-2BY1][SU-BEAMFORMEE][MU-BEAMFORMEE]" --virt-name ap0 --force_channel

Hostapd starts, but the channel width defaults to 40MHz. Below is a screenshot from the app Wifiman, showing analysis of the wifi created.

Screenshot:

3. Using the remaining flags for wider channel:

Command:

sudo ./lnxrouter -o wlp0s20f3 -g 11 --dns 127.0.0.1 --hostname - --ap wlp0s20f3 "Plant Hotspot" -p helios123 -c 149 --country IN --ieee80211n --ieee80211ac --ht_capab "[HT40+][HT40-][SHORT-GI-40][SHORT-GI-20][RX-STBC1][DSSS_CCK-40]" --vht_capab "[MAX-MPDU-11454][VHT160][RXLDPC][SHORT-GI-80][SHORT-GI-160][TX-STBC-2BY1][SU-BEAMFORMEE][MU-BEAMFORMEE]" --virt-name ap0 --force_channel --use_ht --use_vht --vht_channel_width 1 --seg0_center_freq_idx 155

WiFi starts with 80MHz channel as seen in the below screenshot.

Screenshot:

Guide to choosing the flag arguments for 80MHz channel and above:

  1. Get the ht_capab and vht_capab flags right, use the default hostapd config to build it.
Guide * Run `iw list | grep -i -A 15 "band 1"` to get a list of HT Capabilities. Example: ![Screenshot_20230928_131635](https://github.com/garywill/linux-router/assets/60005847/f94baa13-a428-4173-8828-09aed3c98a63) result: [HT40+][HT40-][SHORT-GI-40][SHORT-GI-20][RX-STBC1][DSSS_CCK-40] * Run `iw list | grep -i -A 15 "vht capabilities"` to get a list of VHT Capabilities. Example: ![Screenshot_20230928_132040](https://github.com/garywill/linux-router/assets/60005847/43c1b281-b4cd-4263-abcd-a51ee741ea2e) result: [MAX-MPDU-11454][VHT160][RXLDPC][SHORT-GI-80][SHORT-GI-160][TX-STBC-2BY1][SU-BEAMFORMEE][MU-BEAMFORMEE]
  1. Pick a base channel and check whether your WiFi card can transmit at your desired frequency.

    Guide * Check the specifications for your card, make sure it supports 5GHz N or AC wireless. * Check if the AP can be initiated on the desired channel, using `iw list | grep -E "dBm\)|\(disabled\)"`

  1. Decide whether you want to use 40, 80 or 160MHz channel and pick the corresponding index: 0 for 40MHz 1 for 80MHz 2 for 160MHz 3 for 80+80MHz

  2. From WLAN Channels, in the 5GHz table and under the F0 Index column, pick the index under the required channel width which includes your base channel, called the segment 0 channel.

    Example: Example:

Screenshot_20230928_140808

I wanted 80MHz channel with base channel as 149. The channel index corresponding to those settings is 155.

After collecting the required data, use this format: --use_vht --force_channel --seg0_center_freq_idx <segment 0 channel> -c <base channel> --vht_channel_width <channel width index>

So for my setup: --use_vht --force_channel --seg0_center_freq_idx 155 -c 149 --vht_channel_width 1

Notes:

kphanipavan commented 9 months ago

@garywill

garywill commented 9 months ago

Wonderful! Thank you for PR @kphanipavan !

Although I don't have a 5G wifi hardware to test, it looks great, I'll merge it.

Actually I've been thinking about changing -c <channel>'s behavior. If user specify a channel then the script auto fallbacks to another, that is not wanted. So let's just don't add a --force_channel option, but make -c have --force_channel's feature.

I'll bump version to 0.7.x after merging this

kphanipavan commented 9 months ago

Regarding the force channel behaviour, I found this to be the expected behaviour when I started using your script.

But, IMO, having it the current way can provide a verbose interface to the user. One can suggest a channel with -c, let the script decide if it is valid, and if the user wants to, they can enforce it using --force_channel.

garywill commented 9 months ago

Already in dev branch and I did some modifications and changes. Please share your opinions and comments.

Will merge into master finally

kphanipavan commented 9 months ago

Can I have some more context for 183cf44934a528a20ffa77755e8a419280ae5e5f, other than this, everything looks fine. Tested 0.7.0b just now, working as intended.

garywill commented 9 months ago

When hostapd run, it will output its .conf file path

This is what I get on my PC:

Starting hostapd
Setting /usr/sbin/hostapd to complain mode.
Warning: profile hostapd represents multiple programs
Warning: profile hostapd represents multiple programs
hostapd PID: 5532
Configuration file: /dev/shm/lnxrouter_tmp/lnxrouter.wlan0.conf.oXo0Ky/hostapd.conf
Using interface x0wlan0 with hwaddr 2e:ee:68:xxxxxx and ssid "xxx"
x0wlan0: interface state UNINITIALIZED->ENABLED
x0wlan0: AP-ENABLED
kphanipavan commented 9 months ago

When hostapd run, it will output its .conf file path

Doesn't seem to happen on mine for some reason, tq for clarification.

Also, may I know why the b for version?

garywill commented 9 months ago

Doesn't seem to happen on mine for some reason

Really? What's your hostapd version? To check if I was the only one who see these lines

hostapd PID: 5532
Configuration file: /dev/shm/lnxrouter_tmp/lnxrouter.wlan0.conf.oXo0Ky/hostapd.conf

I searched issues for users' logs. Seems people do have those two lines


The reason of version b (beta) to differ from a stable release is:

  1. -c behavior changed. I can't ensure there's 100% bug-free on the script logic
  2. I myself can't test those 5GHz features (I don't have hardware)

So, we need to give some time for users to test

kphanipavan commented 9 months ago

Hostapd version: v2.11-devel, built from source.

lnxrouter output on my end: ``` linux-router 0.7.0b (https://github.com/garywill/linux-router) Released under LGPL, with no warranty. Use on your own risk. PID: 1143357 Target interface is wlp0s20f3 () PCI: 0000:00:14.3 Network controller [0280]: Intel Corporation Comet Lake PCH CNVi WiFi [8086:06f0] Use random MAC address f4:cf:71:06:68:66 wlp0s20f3 already working in channel 161 (5805 MHz) WARN: Wifi adapter already working in channel 161, which is different than target channel 2 Creating a virtual WiFi interface... ap0 created Setting ap0 new MAC address f4:cf:71:06:68:66 ... haveged_watchdog PID: 1143561 Starting hostapd hostapd PID: 1143566 ap0: interface state UNINITIALIZED->COUNTRY_UPDATE Low entropy detected, starting haveged haveged: command socket is listening at fd 3 iptables v1.8.9 (nf_tables) NOTICE: Not showing all operations done to iptables rules WARN: Since you're using in this mode (specify Internet interface), make sure you've read Notice 1 WARN: You specified Internet interface but this host is providing local DNS. In some unexpected case (eg. mistaken configurations), queries may leak to other interfaces, which you should be aware of. iptables: NAT MASQUERADE all opt -- in * out wlp0s20f3 192.168.11.0/24 !-> 192.168.11.0/24 /* lrt1143357ap0 */ ACCEPT all opt -- in ap0 out wlp0s20f3 192.168.11.0/24 -> 0.0.0.0/0 /* lrt1143357ap0 */ ACCEPT all opt -- in wlp0s20f3 out ap0 0.0.0.0/0 -> 192.168.11.0/24 /* lrt1143357ap0 */ Loaded kernel module nf_nat_pptp iptables: allow DNS ACCEPT tcp opt -- in ap0 out * 192.168.11.0/24 -> 192.168.11.1 tcp dpt:53 /* lrt1143357ap0 */ ACCEPT udp opt -- in ap0 out * 192.168.11.0/24 -> 192.168.11.1 udp dpt:53 /* lrt1143357ap0 */ iptables: allow dhcp ACCEPT udp opt -- in ap0 out * 0.0.0.0/0 -> 0.0.0.0/0 udp dpt:67 /* lrt1143357ap0 */ Starting dnsmasq Oct 5 07:31:08 dnsmasq[1143605]: started, version 2.89 cachesize 150 Oct 5 07:31:08 dnsmasq[1143605]: compile time options: IPv6 GNU-getopt DBus no-UBus i18n IDN2 DHCP DHCPv6 no-Lua TFTP conntrack ipset nftset auth cryptohash DNSSEC loop-detect inotify dumpfile Oct 5 07:31:08 dnsmasq-dhcp[1143605]: DHCP, IP range 192.168.11.10 -- 192.168.11.250, lease time 1h Oct 5 07:31:08 dnsmasq-dhcp[1143605]: DHCP, sockets bound exclusively to interface ap0 Oct 5 07:31:08 dnsmasq[1143605]: using nameserver 127.0.0.1#53 Oct 5 07:31:08 dnsmasq[1143605]: cleared cache dnsmasq PID: 1143605 ap0: interface state COUNTRY_UPDATE->HT_SCAN == Setting up completed, now linux-router should be working == 20/40 MHz operation not permitted on channel pri=2 sec=6 based on overlapping BSSes Fallback to 20 MHz ap0: interface state HT_SCAN->ENABLED ap0: AP-ENABLED ^Cap0: interface state ENABLED->DISABLED ap0: AP-DISABLED ap0: CTRL-EVENT-TERMINATING Doing cleanup.. nl80211: deinit ifname=ap0 disabled_11b_rates=0 Oct 5 07:34:12 dnsmasq[1143605]: exiting on receipt of SIGTERM Killed dnsmasq.pid 1143605 dnsmasq Failed to kill haveged_watchdog.pid 1143561 , it may have exited Killed keep_running.pid 1143635 bash Undoing iptables changes .. Exiting: This is the only running instance Cleaning up done ```

I will see if theres a config option to change for the build.


b now makes sense, cheers...