< All posts

May 16, 2020, 7:34 p.m.

Bypassing the WiFi Tx-Power=20 dBm Limit

Disclaimer: This is for educational purposes only.

Get current country code and txpower:

$ sudo iw reg get

global
country XX: DFS-ETSI
    (2402 - 2482 @ 40), (N/A, 20), (N/A)
    (5170 - 5250 @ 80), (N/A, 20), (N/A), AUTO-BW
    (5250 - 5330 @ 80), (N/A, 20), (0 ms), DFS, AUTO-BW
    (5490 - 5730 @ 160), (N/A, 20), (0 ms), DFS
    (5735 - 5835 @ 80), (N/A, 20), (N/A)

Get interface status:

$ sudo iwconfig
wlanX IEEE 802.11 ESSID:”MySSID”
Mode:Managed Frequency:2.412 GHz Access Point: XX:XX:XX:XX:XX:XX
Bit Rate=6.8 Mb/s Tx-Power=20 dBm
Retry short long limit:2 RTS thr:off Fragment thr:off
Power Management:off
Link Quality=47/70 Signal level=-73 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:0 Missed beacon:0

Changing txpower manually:

$ sudo ifconfig wlanX down
$ sudo iw reg set AS
$ sudo iwconfig wlanX txpower 30
$ sudo ifconfig wlanX up

Change wlan to your interface name.

Countries with txpower 30 (2.4GHz):
country code:

AS
BM
BZ
CA
DM
DO
FM
GD
GT
GU
GY
HT
MH
MP
NI
NZ
PR
PW
TW
US
VE
VI

If you cannot set your adapter’s txpower to greater than 30 or your kernel auto set this based on your router settings. We can force it to just use our own rules.

– Backup old wireless-regd and CRDA
– Recompile wireless-regd
– Recompile CRDA
– Install modified wireless-regd and CRDA

Requirements:

$ sudo apt install build-essential python-m2crypto python-pip libgcrypt20 libgcrypt20-dev libnl-3-dev libnl-genl-3-dev

wireless-regd Source Code:
https://mirrors.edge.kernel.org/pub/software/network/wireless-regdb/

Choose the latest version:wireless-regdb-2020.04.29.tar.xz

CRDA source code:
https://mirrors.edge.kernel.org/pub/software/network/crda/

Choose the latest version:crda-3.18.tar.xz
If you want to verify the authenticity of the downloads, you need to install GNUPG, download the signature file and the public key.

Extract the downloaded files:

$ tar xvf crda-3.18.tar.xz
$ tar xvf wireless-regdb-2019.06.03.tar.xz

Modify the db.txt in the wireless-regdb-2019.06.03 folder, you can now change the txpower based on the country code.

# This is the world regulatory domain
country 00:
    (2402 - 2472 @ 40), (20)
    # Channel 12 - 13.
....

Notes:
00 is the default non-assigned country code for the devices. You can change the (20) to >30 depending on your adapter’s advertised power.
The maintainers have changed the country Bolivia (BO) txpower to 20 from 30. We can change this again to be 30 or higher.

...

country BO: DFS-JP
    (2402 - 2482 @ 40), (20)
    (5250 - 5330 @ 80), (30), DFS
    (5735 - 5835 @ 80), (30)

...

after:

...

country BO: DFS-JP
    (2402 - 2482 @ 40), (36)
    (5250 - 5330 @ 80), (30), DFS
    (5735 - 5835 @ 80), (30)

...

Locating the system’s crda directory.

$ locate regulatory.bin
/lib/crda/regulatory.bin
/usr/share/man/man5/regulatory.bin.5.gz

On Raspbian (Debian) the crda is located at /lib . We need to back it up before installing the new one.

$ sudo tar -cvpf bacup-crda.tar /lib/crda

Copy the newly generated regulatory.bin to /lib/crda/ and your username.key.pub.pem to your extracted crda-3.18/pubkeys directory.

We can now compile and install it. I suggest that you replace the regulatory.bin and libreg.so to /lib/crda instead of running sudo make install.

Build the new regulatory.bin file:

$ make

Generating regulatory.bin digitally signed by username...
./db2bin.py regulatory.bin db.txt ~/.wireless-regdb-username.key.priv.pem
sha1sum db.txt > sha1sum.txt
Generating regulatory.db
./db2fw.py regulatory.db db.txt
Signing regulatory.db (by username)...



GEN  keys-gcrypt.c
Trusted pubkeys: pubkeys/username.key.pub.pem
CC   libreg.so
keys-gcrypt.c:53:32: warning: ‘keys’ defined but not used [-Wunused-const-variable=]
static const struct key_params keys[] = {
                              ^~~~
CC   crda.o
LD   crda
CC   intersect.o
LD   intersect
CC   regdbdump.o
LD   regdbdump
CC   db2rd.o
LD   db2rd
CC   optimize.o
LD   optimize
CHK  /home/username/wireless-regdb-2019.06.03/regulatory.bin

Either install the package using make install or create a debian package with checkinstall:

$ sudo make install

gzip  regulatory.bin.5.gz
gzip  regulatory.db.5.gz
install -m 755 -d //usr/lib/crda
install -m 755 -d //usr/lib/crda/pubkeys
install -m 755 -d //lib/firmware
if [ -f .custom ]; then \
    install -m 644 -t //usr/lib/crda/pubkeys/ username.key.pub.pem; \
fi
install -m 644 -t //usr/lib/crda/pubkeys/ sforshee.key.pub.pem
install -m 644 -t //usr/lib/crda/ regulatory.bin
install -m 644 -t //lib/firmware regulatory.db regulatory.db.p7s
install -m 755 -d //usr/share/man//man5/
install -m 644 -t //usr/share/man//man5/ regulatory.bin.5.gz regulatory.db.5.gz

INSTALL  libreg
INSTALL  libreg-headers
GZIP crda.8
GZIP regdbdump.8
INSTALL  crda
INSTALL  regdbdump
INSTALL  85-regulatory.rules
INSTALL  crda.8.gz
INSTALL  regdbdump.8.gz

Change directory to crda-3.18, edit the Makefile and specify your regulatory.bin location:

REG_BIN?=/home/username/wireless/crda-3.18/regulatory.bin

Also remove the parameter -Werror in Makefile to silence the error:

GEN  keys-gcrypt.c
Trusted pubkeys: pubkeys/username.key.pub.pem
CC   libreg.so
keys-gcrypt.c:53:32: error: ‘keys’ defined but not used [-Werror=unused-const-variable=]
static const struct key_params keys[] = {
                              ^~~~
cc1: all warnings being treated as errors
make: *** [Makefile:119: libreg.so] Error 1

Build and Install crda:

$ make clean
$ make

GEN  keys-gcrypt.c
Trusted pubkeys: pubkeys/username.key.pub.pem
CC   libreg.so
keys-gcrypt.c:53:32: warning: ‘keys’ defined but not used [-Wunused-const-variable=]
static const struct key_params keys[] = {
                              ^~~~
CC   crda.o
LD   crda
CC   intersect.o
LD   intersect
CC   regdbdump.o
LD   regdbdump
CC   db2rd.o
LD   db2rd
CC   optimize.o
LD   optimize
CHK  /home/username/crda-3.18/regulatory.bin

Either use this make install or use checkinstall to generate deb package:

$ sudo make install

INSTALL  libreg
INSTALL  libreg-headers
GZIP crda.8
GZIP regdbdump.8
INSTALL  crda
INSTALL  regdbdump
INSTALL  85-regulatory.rules
INSTALL  crda.8.gz
INSTALL  regdbdump.8.gz

Checkinstall version: (recommended)

crda-3.18]$ sudo checkinstall

checkinstall 1.6.3, Copyright 2010 Felipe Eduardo Sanchez Diaz Duran
           This software is released under the GNU GPL.


The package documentation directory ./doc-pak does not exist. 
Should I create a default set of package docs?  [y]: y

Preparing package documentation...OK

Please write a description for the package.
End your description with an empty line or EOF.
>> crda-mod 
>> 
*****************************************
**** Debian package creation selected ***
*****************************************

This package will be built according to these values: 

0 -  Maintainer: [ root@Pi ]
1 -  Summary: [ crda-mod ]
2 -  Name:    [ crda ]
3 -  Version: [ 3.18 ]
4 -  Release: [ 1 ]
5 -  License: [ GPL ]
6 -  Group:   [ checkinstall ]
7 -  Architecture: [ armv7 ]
8 -  Source location: [ crda-3.18 ]
9 -  Alternate source location: [  ]
10 - Requires: [  ]
11 - Recommends: [  ]
12 - Suggests: [  ]
13 - Provides: [ crda ]
14 - Conflicts: [  ]
15 - Replaces: [  ]

Enter a number to change any of them or press ENTER to continue: 

Installing with make install...

========================= Installation results ===========================
  INSTALL  libreg
  INSTALL  libreg-headers
 GZIP crda.8
 GZIP regdbdump.8
  INSTALL  crda
  INSTALL  regdbdump
  INSTALL  85-regulatory.rules
  INSTALL  crda.8.gz
  INSTALL  regdbdump.8.gz

======================== Installation successful ==========================

Copying documentation directory...
./
./LICENSE
./README

Copying files to the temporary directory...OK

Stripping ELF binaries and libraries...OK

Compressing man pages...OK

Building file list...OK

Building Debian package...OK

Installing Debian package...OK

Erasing temporary files...OK

Writing backup package...OK

Deleting temp dir...OK


**********************************************************************

 Done. The new package has been installed and saved to

 /home/user/git/wifi/crda-3.18/crda_3.18-1_amd64.deb

 You can remove it from your system anytime using: 

      dpkg -r crda

**********************************************************************

Reboot your system, issue the iw reg set txpower 30* to your interface and check it with iwconfig.
Enjoy!

wlanX     IEEE 802.11  ESSID:"SampleSSID"
          Mode:Managed  Frequency:2.412 GHz  Access Point: XX:XX:XX:XX:XX:XX
          Bit Rate=67 Mb/s   Tx-Power=36 dBm
          Retry short  long limit:2   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=63/70  Signal level=-43 dBm
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0

References:
https://askubuntu.com/questions/597546/iwconfig-wlan0-txpower-30mw-not-working
https://null-byte.wonderhowto.com/how-to/ultimate-guide-upping-tx-power-kali-linux-2-0-0168325/
https://raspiblog.noblogs.org/post/2019/08/04/bypassing-the-wifi-tx-power20-dbm-limit/