HEX
Server: LiteSpeed
System: Linux srv146.niagahoster.com 4.18.0-553.30.1.lve.el8.x86_64 #1 SMP Tue Dec 3 01:21:19 UTC 2024 x86_64
User: kodi1989 (1633)
PHP: 8.1.34
Disabled: symlink,shell_exec,exec,popen,system,dl,passthru,escapeshellarg,escapeshellcmd,show_source,pcntl_exec
Upload Files
File: //opt/alt/python37/lib/python3.7/site-packages/pyroute2/__pycache__/iwutil.cpython-37.pyc
B

��fc[�@sbdZddlZddlmZmZmZddlmZmZm	Z	m
Z
mZmZm
Z
e�e�ZGdd�de
�ZdS)u�
IW module
=========

Experimental wireless module — nl80211 support.

Disclaimer
----------

Unlike IPRoute, which is mostly usable, though is far from
complete yet, the IW module is in the very initial state.
Neither the module itself, nor the message class cover the
nl80211 functionality reasonably enough. So if you're
going to use it, brace yourself — debug is coming.

Messages
--------

nl80211 messages are defined here::

    pyroute2/netlink/nl80211/__init__.py

Pls notice NLAs of type `hex`. On the early development stage
`hex` allows to inspect incoming data as a hex dump and,
occasionally, even make requests with such NLAs. But it's
not a production way.

The type `hex` in the NLA definitions means that this
particular NLA is not handled yet properly. If you want to
use some NLA which is defined as `hex` yet, pls find out a
specific type, patch the message class and submit your pull
request on github.

If you're not familiar with NLA types, take a look at RTNL
definitions::

    pyroute2/netlink/rtnl/ndmsg.py

and so on.

Communication with the kernel
-----------------------------

There are several methods of the communication with the kernel.

    * `sendto()` — lowest possible, send a raw binary data
    * `put()` — send a netlink message
    * `nlm_request()` — send a message, return the response
    * `get()` — get a netlink message
    * `recv()` — get a raw binary data from the kernel

There are no errors on `put()` usually. Any `permission denied`,
any `invalid value` errors are returned from the kernel with
netlink also. So if you do `put()`, but don't do `get()`, be
prepared to miss errors.

The preferred method for the communication is `nlm_request()`.
It tracks the message ID, returns the corresponding response.
In the case of errors `nlm_request()` raises an exception.
To get the response on any operation with nl80211, use flag
`NLM_F_ACK`.

Reverse it
----------

If you're too lazy to read the kernel sources, but still need
something not implemented here, you can use reverse engineering
on a reference implementation. E.g.::

    # strace -e trace=network -f -x -s 4096 \
            iw phy phy0 interface add test type monitor

Will dump all the netlink traffic between the program `iw` and
the kernel. Three first packets are the generic netlink protocol
discovery, you can ignore them. All that follows, is the
nl80211 traffic::

    sendmsg(3, {msg_name(12)={sa_family=AF_NETLINK, ... },
        msg_iov(1)=[{"\x30\x00\x00\x00\x1b\x00\x05 ...", 48}],
        msg_controllen=0, msg_flags=0}, 0) = 48
    recvmsg(3, {msg_name(12)={sa_family=AF_NETLINK, ... },
        msg_iov(1)=[{"\x58\x00\x00\x00\x1b\x00\x00 ...", 16384}],
        msg_controllen=0, msg_flags=0}, 0) = 88
    ...

With `-s 4096` you will get the full dump. Then copy the strings
from `msg_iov` to a file, let's say `data`, and run the decoder::

    $ pwd
    /home/user/Projects/pyroute2
    $ export PYTHONPATH=`pwd`
    $ python scripts/decoder.py pyroute2.netlink.nl80211.nl80211cmd data

You will get the session decoded::

    {'attrs': [['NL80211_ATTR_WIPHY', 0],
               ['NL80211_ATTR_IFNAME', 'test'],
               ['NL80211_ATTR_IFTYPE', 6]],
     'cmd': 7,
     'header': {'flags': 5,
                'length': 48,
                'pid': 3292542647,
                'sequence_number': 1430426434,
                'type': 27},
     'reserved': 0,
     'version': 0}
    {'attrs': [['NL80211_ATTR_IFINDEX', 23811],
               ['NL80211_ATTR_IFNAME', 'test'],
               ['NL80211_ATTR_WIPHY', 0],
               ['NL80211_ATTR_IFTYPE', 6],
               ['NL80211_ATTR_WDEV', 4],
               ['NL80211_ATTR_MAC', 'a4:4e:31:43:1c:7c'],
               ['NL80211_ATTR_GENERATION', '02:00:00:00']],
     'cmd': 7,
     'header': {'flags': 0,
                'length': 88,
                'pid': 3292542647,
                'sequence_number': 1430426434,
                'type': 27},
     'reserved': 0,
     'version': 1}

Now you know, how to do a request and what you will get as a
response. Sample collected data is in the `scripts` directory.

Submit changes
--------------

Please do not hesitate to submit the changes on github. Without
your patches this module will not evolve.
�N)�	NLM_F_ACK�
NLM_F_DUMP�
NLM_F_REQUEST)�BSS_STATUS_NAMES�
CHAN_WIDTH�IFTYPE_NAMES�NL80211�
NL80211_NAMES�SCAN_FLAGS_NAMES�
nl80211cmdcseZdZ�fdd�Zdd�ZdCdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdDdd�Zdd�ZdEd d!�ZdFd#d$�ZdGd%d&�ZdHd(d)�ZdId*d+�Zd,d-�Zd.d/�ZdJd0d1�Zd2d3�ZdKd4d5�Zd6d7�ZdLd8d9�Zd:d;�Zd<d=�Zd>d?�Ze d@�dAdB�Z!�Z"S)M�IWcs�d|kr|d}|d=nd}d|kr<t�d�|�d�|d<d|krP|�d�}nd}|dkrh|rddnd}tt|�j||�|j||d�dS)	N�groups�asynczJuse "async_cache" instead of "async", "async" is a keyword from Python 3.7�async_cacheF���r)r)�log�warning�pop�superr�__init__�bind)�self�argv�kwargr
r)�	__class__��@/opt/alt/python37/lib/python3.7/site-packages/pyroute2/iwutil.pyr�szIW.__init__cCs:t�}td|d<d|gg|d<|j||jttBd�dS)uP
        Delete a virtual interface

            - dev — device index
        ZNL80211_CMD_DEL_INTERFACE�cmd�NL80211_ATTR_IFINDEX�attrs)�msg_type�	msg_flagsN)rr	�nlm_request�pridrr)r�dev�msgrrr�
del_interface�s
zIW.del_interfaceNrcCs�t�||�}t|t�std��t�}td|d<d|gd|gg|d<|dk	r`|d�d|g�n$|dk	r||d�d	|g�ntd
��|j||j	t
tBd�dS)uB
        Create a virtual interface

            - ifname — name of the interface to create
            - iftype — interface type to create
            - dev — device index
            - phy — phy index

        One should specify `dev` (device index) or `phy`
        (phy index). If no one specified, phy == 0.

        `iftype` can be integer or string:

        1. adhoc
        2. station
        3. ap
        4. ap_vlan
        5. wds
        6. monitor
        7. mesh_point
        8. p2p_client
        9. p2p_go
        10. p2p_device
        11. ocb
        ziftype must be intZNL80211_CMD_NEW_INTERFACEr�NL80211_ATTR_IFNAME�NL80211_ATTR_IFTYPErNr�NL80211_ATTR_WIPHYzno device specified)r r!)r�get�
isinstance�int�	TypeErrorrr	�appendr"r#rr)rZifname�iftyper$Zphyr%rrr�
add_interface�s
zIW.add_interfacecCs|��S)z9
        Get list of all wifi network interfaces
        )�get_interfaces_dump)rrrr�list_dev�szIW.list_devcCs(t�}td|d<|j||jttBd�S)z-
        Get list of all phy devices
        ZNL80211_CMD_GET_WIPHYr)r r!)rr	r"r#rr)rr%rrr�
list_wiphy�sz
IW.list_wiphycCsd|�d�S)Nzphy%ir))�get_attr)r�attrrrr�
_get_phy_nameszIW._get_phy_namecCs|�d�pdS)N�NL80211_ATTR_WIPHY_FREQr)r4)rr5rrr�_get_frequencyszIW._get_frequencycCshi}x^|��D]R}|�d�}|dk	r.|�|�nd}|�d�}|�d�|�|�|�d�||g||<qW|S)z+
        Get interfaces dictionary
        �NL80211_ATTR_CHANNEL_WIDTHNrr'r�NL80211_ATTR_MAC)r1r4r8r6)r�retZwifZ
chan_width�freqZwifnamerrr�get_interfaces_dicts

zIW.get_interfaces_dictcCs(t�}td|d<|j||jttBd�S)z%
        Get interfaces dump
        �NL80211_CMD_GET_INTERFACEr)r r!)rr	r"r#rr)rr%rrrr1szIW.get_interfaces_dumpcCs6t�}td|d<d|gg|d<|j||jttBd�S)zO
        Get interface by phy ( use x.get_attr('NL80211_ATTR_WIPHY') )
        r>rr)r)r r!)rr	r"r#rr)rr5r%rrr�get_interface_by_phy's
zIW.get_interface_by_phycCs2t�}td|d<d|gg|d<|j||jtd�S)zS
        Get interface by ifindex ( use x.get_attr('NL80211_ATTR_IFINDEX')
        r>rrr)r r!)rr	r"r#r)r�ifindexr%rrr�get_interface_by_ifindex2s
zIW.get_interface_by_ifindexcCs6t�}td|d<d|gg|d<|j||jttBd�S)z)
        Get stations by ifindex
        ZNL80211_CMD_GET_STATIONrrr)r r!)rr	r"r#rr)rr@r%rrr�get_stations=s
zIW.get_stationsFc	
Cs4t�}	td|	d<d|gd|gd|gg|	d<|r�|	d�ddg�t�||�}t|t�s`td	��|d
kr�|r�|	d�d|g�|	d�d|g�nl|d
kr�|r�|r�|	d�d|g�|	d�d|g�|	d�d|g�n$|dkr�|	d�d|g�ntd��|dk	�r|	d�d|g�|j|	|j	t
tBd�dS)a�
        Connect to network by ssid
            - ifindex - IFINDEX of the interface to perform the connection
            - ssid - Service set identification
            - freq - Frequency in MHz
            - bssid - The MAC address of target interface
            - channel_fixed: Boolean flag
            - width - Channel width
            - center - Central frequency of the 40/80/160 MHz channel
            - center2 - Center frequency of second segment if 80P80

        If the flag of channel_fixed is True, one should specify both the width
        and center of the channel

        `width` can be integer of string:

        0. 20_noht
        1. 20
        2. 40
        3. 80
        4. 80p80
        5. 160
        6. 5
        7. 10
        ZNL80211_CMD_JOIN_IBSSrr�NL80211_ATTR_SSIDr7rZNL80211_ATTR_FREQ_FIXEDNzwidth must be int)���r9ZNL80211_ATTR_CENTER_FREQ1�ZNL80211_ATTR_CENTER_FREQ2)r���zNo channel specifiedr:)r r!)rr	r.rr*r+r,r-r"r#rr)
rr@�ssidr<�bssidZ
channel_fixed�width�centerZcenter2r%rrr�	join_ibssHs0%

zIW.join_ibsscCs:t�}td|d<d|gg|d<|j||jttBd�dS)zS
        Leave the IBSS -- the IBSS is determined by the network interface
        ZNL80211_CMD_LEAVE_IBSSrrr)r r!N)rr	r"r#rr)rr@r%rrr�
leave_ibss�s
z
IW.leave_ibsscCsRt�}td|d<d|gd|gd|gd|gd|gg|d<|j||jttBd	�d
S)z:
        Send an Authentication management frame.
        ZNL80211_CMD_AUTHENTICATErrr:rCr7ZNL80211_ATTR_AUTH_TYPEr)r r!N)rr	r"r#rr)rr@rLrKr<Z	auth_typer%rrr�authenticate�szIW.authenticaterHcCsFt�}td|d<d|gd|gd|gg|d<|j||jttBd�dS)	z;
        Send a Deauthentication management frame.
        ZNL80211_CMD_DEAUTHENTICATErrr:�NL80211_ATTR_REASON_CODEr)r r!N)rr	r"r#rr)rr@rL�reason_coder%rrr�deauthenticate�szIW.deauthenticatecCsft�}td|d<d|gd|gd|gd|gg|d<|dk	rL|d�d	|g�|j||jttBd
�dS)z4
        Send an Association request frame.
        ZNL80211_CMD_ASSOCIATErrr:rCr7rNZNL80211_ATTR_IE)r r!)rr	r.r"r#rr)rr@rLrKr<Z
info_elementsr%rrr�	associate�szIW.associaterEcCsFt�}td|d<d|gd|gd|gg|d<|j||jttBd�dS)	z9
        Send a Disassociation management frame.
        ZNL80211_CMD_DISASSOCIATErrr:rRr)r r!N)rr	r"r#rr)rr@rLrSr%rrr�disassociate�szIW.disassociatecCsZt�}td|d<d|gd|gg|d<|dk	r@|d�d|g�|j||jttBd�dS)	z7
        Connect to the ap with ssid and bssid
        ZNL80211_CMD_CONNECTrrrCrNr:)r r!)rr	r.r"r#rr)rr@rKrLr%rrr�connect�sz
IW.connectcCs:t�}td|d<d|gg|d<|j||jttBd�dS)z'
        Disconnect the device
        ZNL80211_CMD_DISCONNECTrrr)r r!N)rr	r"r#rr)rr@r%rrr�
disconnect�s
z
IW.disconnectcCs6t�}td|d<d|gg|d<|j||jttBd�S)z)
        Return the survey info.
        ZNL80211_CMD_GET_SURVEYrrr)r r!)rr	r"r#rr)rr@r%rrr�survey�s
z	IW.surveyc
Cst�}|��|�d�t�}td|d<d|gg|d<|dk	r\t|t�r\|d�d|g�d}|r�|td	O}|d�d
|g�|j	||j
ttBd�d}x0|r�|�
�}x|D]}|d
dkr�d}Pq�Wq�W|��t�}	td|	d<d|gg|	d<|j	|	|j
ttBd�S)z�
        Trigger scan and get results.

        Triggering scan usually requires root, and can take a
        couple of seconds.
        �scanZNL80211_CMD_TRIGGER_SCANrrrNZNL80211_ATTR_SCAN_SSIDSrZNL80211_SCAN_FLAG_FLUSHZNL80211_ATTR_SCAN_FLAGS)r r!T�eventZNL80211_CMD_NEW_SCAN_RESULTSF�NL80211_CMD_GET_SCAN)rrZadd_membershiprr	r+�listr.r
r"r#rrr*�closer)
rr@ZssidsZflush_cacheZnsockr%Z
scan_flagsZscanResultNotFoundZlistMsgZmsg2rrrrZ
s8




zIW.scancCs|t�}td|d<d|gg|d<|j||jttBd�}x@|D]8}|�d�}|dk	r<|�d�}|td	td
fkr<|Sq<WdS)z�
        Returns the same info like scan() does, but only about the
        currently associated BSS.

        Unlike scan(), it returns immediately and doesn't require root.
        r\rrr)r r!ZNL80211_ATTR_BSSNZNL80211_BSS_STATUSZ
associatedZibss_joined)rr	r"r#rrr4r)rr@r%�res�xZattr_bss�statusrrr�get_associated_bss?s



zIW.get_associated_bsscCsHt�}td|d<t}|dkr(|tO}nd|gg|d<|j||j|d�S)z�
        Get regulatory domain information. If attr specified, get regulatory
        domain information for this device
        ( use x.get_attr('NL80211_ATTR_WIPHY') ).
        ZNL80211_CMD_GET_REGrNr)r)r r!)rr	rrr"r#)rr5r%�flagsrrr�get_regulatory_domain`s
zIW.get_regulatory_domaincCs:t�}td|d<d|gg|d<|j||jttBd�dS)z(
        Set regulatory domain.
        ZNL80211_CMD_REQ_SET_REGrZNL80211_ATTR_REG_ALPHA2r)r r!N)rr	r"r#rr)rZalpha2r%rrr�set_regulatory_domainps
zIW.set_regulatory_domaincCsZt�}td|d<d|gd|gg|d<|dk	r@|d�d|g�|j||jttBd�dS)	u�
        Set TX power of interface.

            - dev — device index
            - mode — TX power setting (0 - auto, 1 - limit, 2 - fixed)
            - mbm — TX power in mBm (dBm * 100)
        ZNL80211_CMD_SET_WIPHYrrZ#NL80211_ATTR_WIPHY_TX_POWER_SETTINGrNZ!NL80211_ATTR_WIPHY_TX_POWER_LEVEL)r r!)rr	r.r"r#rr)rr$�modeZmbmr%rrr�set_tx_power|szIW.set_tx_powercCs@t�}td|d<d|gd|gg|d<|j||jttBd�dS)zK
        Set wiphy network namespace to process network namespace.
        �NL80211_CMD_SET_WIPHY_NETNSrr)ZNL80211_ATTR_PIDr)r r!N)rr	r"r#rr)r�wiphy�pidr%rrr�set_wiphy_netns_by_pid�szIW.set_wiphy_netns_by_pidcCs@t�}td|d<d|gd|gg|d<|j||jttBd�dS)zL
        Set wiphy network namespace to namespace referenced by fd.
        rhrr)ZNL80211_ATTR_NETNS_FDr)r r!N)rr	r"r#rr)rriZnetns_fdr%rrr�set_wiphy_netns_by_fd�szIW.set_wiphy_netns_by_fdcCs^t�||�}t|t�std��t�}td|d<d|gd|gg|d<|j||jt	t
Bd�dS)	uk
        Set interface type
            - ifindex — device index
            - iftype — interface type

        `iftype` can be integer or string:
        1. adhoc
        2. station
        3. ap
        4. ap_vlan
        5. wds
        6. monitor
        7. mesh_point
        8. p2p_client
        9. p2p_go
        10. p2p_device
        11. ocb
        ziftype must be intZNL80211_CMD_SET_INTERFACErrr(r)r r!N)rr*r+r,r-rr	r"r#rr)rr@r/r%rrr�set_interface_type�s
zIW.set_interface_type)�returncCsZ|�|�}d}x|D]}|�d�}qW|dk	rRx&t��D]\}}||kr8|}q8Wnd}|S)z,
        return interface type name
        Nr(zNot Found Type)rAr4r�items)rr@�dump�type�d�key�valuer_rrr�get_interface_type�s


zIW.get_interface_type)Nr)NFNNN)r)rH)N)rE)N)NF)N)N)#�__name__�
__module__�__qualname__rr&r0r2r3r6r8r=r1r?rArBrOrPrQrTrUrVrWrXrYrZrbrdrergrkrlrm�strru�
__classcell__rr)rrr�sD#
/


<





5!

$r)�__doc__�loggingZpyroute2.netlinkrrrZpyroute2.netlink.nl80211rrrrr	r
r�	getLoggerrvrrrrrr�<module>�s
$