WIFI function tools and operations in Linux

wifi module driver

The first step in using the system wifi function is to determine the model of the device wifi module and install the corresponding driver. In embedded development, it is generally necessary to obtain the driver source code of the wifi module, and then cross-compile to build the driver module and install it using the insmod command.

Generally speaking, the WIFI module has two working modes:

  • Station Mode: Station mode, used to access APs such as routers
  • AP Mode: Access point mode, used to be accessed

The wifi driver codes of different modes may be different, so it may be necessary to install two driver modules to support the functions of these two modes. For details, please refer to the manual and guide document of the corresponding wifi module.

main tool

There are two main wifi configuration tools used in Linux systems:

  • wireless tools: including iwconfig, iwlist, iwspy, iwpriv and other tools, mainly configure wep encrypted wifi
  • wpa_supplicant: including wpa_cli, wpa_supplicant and other tools, mainly configure wpa encrypted wifi

Because most of the commonly used wifi networks are encrypted by the wpa protocol, the wpa_supplicant tool is used more, but the wireless tools tool is still very useful in the management of the wifi interface.

wireless tools

This toolkit is one of the user-level configuration tools for Linux Wireless Extensions (LWE). LWE is a tool for wireless network configuration under Linux, including kernel support, user layer configuration tool and driver interface support. At present, many wireless network cards support LWE, and mainstream Linux distributions, such as Redhat Linux and Ubuntu Linux, have already brought this configuration tool.

After compiling and building, commands are generated: iwconfg, iwlist, iwspy, iwpriv, etc. The use of these commands depends on the generated dynamic library file: libiw.so.29, so when copying these commands to the target machine, remember to add the library file together Copy it to the lib directory of the system, otherwise it will prompt that the library file cannot be found.

iwconfig is the main tool of LWE, which can configure most of the parameters of the wireless network card. The specific command parameters and options can be viewed through the help command. Some of these parameters are explained as follows:

  • essid: Set the ESSID (Extension Service Set ID) of the wireless network card. Different wireless networks are distinguished by ESSID. Under normal circumstances, only wireless stations with the same ESSID can communicate with each other, unless you want to monitor the wireless network. The following parameter is the ESSID string enclosed in double quotes, or any/on/off. If the ESSID string contains any/no/off, you need to add "--" in front.

  • mode: Set the working mode of the wireless network card, which can be

    • Ad-hoc: peer-to-peer wireless network without AP
    • Managed: Through a network composed of multiple APs, wireless devices can roam in this network
    • Master: Set the wireless network card as an AP
    • Repeater: Set as a wireless network relay device, which can forward network packets
    • Secondary: AP/Repeater set as backup
    • Monitor: monitor mode
    • Auto: The working mode is automatically selected by the wireless network card
  • freq/channel: Set the working frequency or channel of the wireless network card. The parameter less than 1000 is considered as the channel, and the parameter greater than 10000 is considered as the frequency. The frequency unit is Hz, and the order of magnitude can be changed by appending k, M, G after the number, such as 2.4G. Channels start at 1. Use the iwlist tool to view the frequencies and channels supported by the wireless network card. The parameter off/auto instructs the wireless network to automatically pick frequencies. Note: If it is in Managed mode, the AP will indicate the working frequency of the wireless network card, so the parameters of this setting will be ignored. In Ad-hoc mode, only the set frequency is used to initialize the wireless network. If you join an existing Ad-hoc network, the set frequency parameter will be ignored.

  • ap: Connect to the specified AP or wireless network. The following parameter can be the MAC address of the AP or the identifier obtained by iwlist scan. If it is Ad-hoc, connect to an existing Ad-hoc network. Use the off parameter to let the wireless network card enter the automatic mode without changing the currently connected AP. any/auto parameter, the wireless network card automatically selects the best AP. Note: If the wireless signal is low to a certain level, the wireless network will enter the automatic AP selection mode.

  • rate/bit: If the wireless network card supports multiple rates, this command can be used to set the working rate. The parameter less than 1000 is defined by the specific wireless network card driver, which is generally the index value of the transmission rate, and the parameter greater than 1000 is the rate, in bps, and k, M, G can be appended to the number to specify the order of magnitude. The auto parameter makes the wireless network card automatically select the rate. The fixed parameter makes the wireless network card not use the automatic rate mode.

  • txpower: If the wireless network card supports multiple transmit power settings, use this parameter to set the transmit, and the unit is dBm. If it is specified as W (milliwatt), the conversion formula is: dBm=30+log(W). The parameters on/off can turn the transmitting unit on and off, and auto and fixed specify whether the wireless automatically selects the transmit power.

  • sens: Set the lower limit of the receiving sensitivity. Below the lower limit, the wireless network card thinks that the wireless network signal is too poor. Different network cards will take different measures. Some modern wireless network cards will automatically select a new AP. The positive parameter is raw data, which is directly passed to the wireless network card driver for processing, which is generally considered to be a percentage. Negative values ​​represent dBm values.

  • retry: Set the retransmission mechanism of the wireless network card. limit 'value' specifies the maximum number of retransmissions; lifetime 'value' specifies the maximum retry time in seconds, which can be specified in milliseconds and microseconds with m and u. If the wireless network card supports automatic mode, you can also append min and max before limit and lifetime to specify the upper and lower limit values.

  • rts: Specifies the RTS/CTS handshake method. Using the RTS/CTS handshake will increase the overhead, but if there are hidden wireless nodes in the wireless network or there are many wireless nodes, the performance can be improved. The following parameter specifies a minimum packet size that uses this mechanism. If the value is equal to the maximum packet size, it is equivalent to prohibiting the use of this mechanism. Auto/off/fixed parameters can be used.

  • frag: Set the fragment size for sending packets. Setting up fragmentation adds overhead, but can improve packet arrival rates in noisy environments. In general, this parameter is less than the maximum packet size. Some wireless network cards that support Burst mode can set a value larger than the maximum packet size to allow Burst mode. You can also use auto/fixed/off parameters.

  • key/enc[ryption]: Set the encryption key used by the wireless network card. Here is the encryption key for setting the WEP mode. If you want to use WPA, you need the wpa_supplicant toolkit. The key parameter can be a hexadecimal value in the format of XXXX-XXXX-XXXX-XXXX or XXXXXXXX, or it can be an ASCII character of s:xxxxxx. If [index] is added before the key parameter, only the key corresponding to the index value is set, and the current key is not changed. Specifying the [index] value directly can set which key is currently used. Specify on/off to control whether encryption mode is used. open/restricted specifies the encryption mode, depending on the wireless network card, the open mode of most wireless network cards does not use encryption and allows to receive packets without encryption, and the restricted mode uses encryption. Multiple key parameters can be used, but only the last one takes effect. A WEP key can be 40bit, represented by 10 hexadecimal digits or 5 ASCII characters, or 128bit, represented by 26 hexadecimal digits or 13 ASCII characters.

  • power: Set the power management mode of the wireless network card. period 'value' specifies the wake-up period, timeout 'value' specifies the waiting time for entering sleep, and min and max can be added before these two parameters. The unit of these values ​​is seconds, and m and u can be appended to specify milliseconds and microseconds . The off/on parameter specifies whether to allow power management, and all/unicast/multicast specifies the type of packets that are allowed to wake up.

  • commit: Submit all parameter modifications to the wireless network card driver. Some wireless network card drivers will cache the wireless network card parameter modification first, use this command to make the wireless network card parameter modification take effect. However, it is generally not necessary to use this command, because the wireless network card driver will eventually take effect for the modification of the parameters, which is generally used in debugging.

Examples of common usage of these tool commands:

  1. Scan for available wireless networks

     iwlist [wifi-name] scanning
    

    Among them, wifi-name is the name of the wireless network card. For example, the network card eth0 is the default network card name of the system. The wifi-name can be viewed with ifconfig, generally ra0 (wireless network card), but ifconfig ra0 up is required.

  2. Scan network information

    iwlist ra0 scan
    ===<rt_ioctl_giwscan.11(11) BSS returned, data-<length = 1427
    ra0      Scan completed :
    Cell 01 - Address:C4:CA:D9:1D:9E:A0
    Protocol:802.11b/g/n
    ESSID:""
    Mode:Managed
    Frequency:2.412 GHz(Channel 1)
    Quality=7/100  Signal level=-87 dBm  Noise level=-82 dBm
    Encryption key:off
    Bit Rates:54 Mb/s
    

    As above: The value of the ESSID item is the name of the wireless network, such as "TXJ" above. If the value is empty, it means that the option to hide the ESSID is checked in the wireless router. The ESSID is very critical. If it is hidden, it is not that it cannot be connected, but it needs to be cracked. It is very troublesome to use other tools.

  3. Set the type of network to connect to

     iwpriv ra0 set NetworkType=Infra
    
  4. Set the safe mode of the wireless network you want to connect to

     iwpriv ra0 set AuthMode=WPA2PSK
    
  5. Set the network encryption method: (CCMP is AES)

     iwpriv ra0 set EncrypType=TKIP
    
  6. Set password when connecting

     iwprivra0 set WPAPSK=*******  //********为加密网络的密码
    
  7. Connect to this network: There are two ways:

     iwpriv ra0 set SSID=TXJ
     iwconfig ra0 essid TXJ
    

    At this point, if the password is correct, you can connect to the network TXJ.

  8. View status

     iwpriv ra0 connStatus
    

    Of course, you can also check the status with the iwconfig command.

wpa_supplicant

wpa_supplicant is one of the commonly used tools, but the disadvantage is that it does not support all drivers. Please visit the wpa_supplicant website for a list of supported drivers. wpa_supplicant can currently only connect to those wireless networks for which you have configured the ESSID. wireless-tools supports almost all wireless cards and drivers, but it cannot connect to APs that only support WPA.

The compiled wpa_supplicant source program can see two main executable tools: wpa_supplicant and wpa_cli. wpa_supplicant is the core program, and its relationship with wpa_cli is the relationship between server and client: run wpa_supplicant in the background, and use wpa_cli to search, set, and connect to the network.

The specific steps to use wpa_supplicant to connect to wifi are as follows:

  1. Start the wpa_supplicant daemon:

    /sbin/wpa_supplicant -d -Dwext -iwlan0 -c /etc/wpa_supplicant.conf

    in:

     -d :增加调试信息
     -Dwext :wext,驱动名称
     -iwlan0 :wlan0,网络接口名称
     /etc/wpa_supplicant.conf :配置文件
    

    The content of the configuration file is roughly as follows:

       ctrl_interface=/var/run/wpa_supplicant  
       ctrl_interface_group=wheel  
       update_config=1
       network={  
            ssid="test-ap"
            scan_ssid=1
            psk=37bca5cf24a345f514d319211822f568bba28f8f0b74c894e7644
            proto=RSN  
            key_mgmt=WPA-PSK  
            pairwise=CCMP  
            auth_alg=OPEN  
       }  
    

    The content in the network is the configuration of the access AP. When wpa_supplicant starts, it will use this configuration to connect to the wifi. Several options are described below, and more explanations can be found in the official configuration file comments.

    • ssid access point name
    • scan_ssid=1 This is required if your wireless access point is hidden.
    • psk=xx is the encrypted password, which is automatically generated by wpa_passphrase
    • proto=RSN WPA2 is just an alias for RSN, supports WPA and WPA2

    Regarding the generation of psk encryption, use the wpa_passphrase command as follows:

    $ wpa_passphrase TPLINK 12345678
     network={  
         ssid="TPLINK"
         #psk="12345678"
         psk=992194d7a6158009bfa25773108291642f28a0c32a31ab2556a15dee97ef0dbb
     } 
    

    This represents the access point named TPLINK, the password is 12345678, and the output is the configuration content of the access point in wpa_supplicant.conf.

  2. Execute the wpa_cli tool to search and connect

     wpa_cli -i wlan0
    

    wlan0 is the name of the wifi interface, the above list will enter the interactive mode, and then perform specific actions. The specific supported commands can be viewed through the help command. The following are some commonly used commands.

    • scan scans the list of currently available WiFi
    • scan_result View the scan results of the last scan
    • add_network Add an AP to connect to the network
    • set_network Set related parameters for connecting to the network
    • get_network Get the parameters of the connected network
    • remove_network removes a connected network
    • enable_network connect to the specified AP
    • disable_network disables a network
    • The current AP connection on the disconnet port
    • status View current connection status information
    • save_config save the configuration

    Some examples of interactive connections:

    • Connect to an unencrypted AP

        add_network
        set_network 0 ssid "ap1"
        set_network 0 key_mgmt NONE
        enable_network 0
        quit
      
    • Connect to WEP encrypted AP

        add_network
        set_network 1 ssid "ap2"
        set_network 1 key_mgmt NONE
        set_network 1 wep_key0 "your ap password"
        enable_network 1
      
    • Connect to WPA-PSK/WPA2-PSK encrypted AP

        add_network
        set_network 2 ssid "ap3"
        set_network 2 psk "your pre-shared key"
        enable_network 2
      

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324373704&siteId=291194637