wpa_supplicant软件架构分析

Posted OracleLoyal

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpa_supplicant软件架构分析相关的知识,希望对你有一定的参考价值。

技术分享

 

wpa_supplicant软件架构分析

1. 启动命令

wpa supplicant 在启动时,启动命令可以带有很多参数,目前我们的启动命令如下:

wpa_supplicant /system/bin/wpa_supplicant -Dwext -ieth0 -c/data/wifi/wpa_supplicant.conf -f/data/wifi/wpa_log.txt

 

wpa_supplicant对于启动命令带的参数,用了两个数据结构来保存,

一个是 wpa_params, 另一个是wpa_interface.

这主要是考虑到wpa_supplicant是可以同时支持多个网络接口的。

wpa_params数据结构主要记录与网络接口无关的一些参数设置。

而每一个网络接口就用一个wpa_interface数据结构来记录。

在启动命令行中,可以用-N来指定将要描述一个新的网络接口,对于一个新的网络接口,可以用下面六个参数描述:

-i<ifname> : 网络接口名称

-c<conf>: 配置文件名称

-C<ctrl_intf>: 控制接口名称

-D<driver>: 驱动类型

-p<driver_param>: 驱动参数

-b<br_ifname>: 桥接口名称

 

2. wpa_supplicant 初始化流程

2.1. main()函数:

在这个函数中,主要做了四件事。

a. 解析命令行传进的参数。

b. 调用wpa_supplicant_init()函数,做wpa_supplicant的初始化工作。

c. 调用wpa_supplicant_add_iface()函数,增加网络接口。

d. 调用wpa_supplicant_run()函数,让wpa_supplicant真正的run起来。

 

2.2. wpa_supplicant_init()函数:

a. 打开debug 文件。

b. 注册EAP peer方法。

c. 申请wpa_global内存,该数据结构作为统领其他数据结构的一个核心, 主要包括四个部分:

wpa_supplicant *ifaces   /*每个网络接口都有一个对应的wpa_supplicant数据结构,该指针指向最近加入的一个,在wpa_supplicant数据结构中有指针指向next*/

wpa_params params   /*启动命令行中带的通用的参数*/

ctrl_iface_global_priv *ctrl_iface  /*global 的控制接口*/

ctrl_iface_dbus_priv *dbus_ctrl_iface  /*dbus 的控制接口*/

d. 设置wpa_global中的wpa_params中的参数。

e. 调用eloop_init函数将全局变量eloop中的user_data指针指向wpa_global。

f. 调用wpa_supplicant_global_ctrl_iface_init函数初始化global 控制接口。

g. 调用wpa_supplicant_dbus_ctrl_iface_init函数初始化dbus 控制接口。

h. 将该daemon的pid写入pid_file中。

 

2.3. wpa_supplicant_add_iface()函数:

该函数根据启动命令行中带有的参数增加网络接口, 有几个就增加几个。

a. 因为wpa_supplicant是与网络接口对应的重要的数据结构,所以,首先分配一个wpa_supplicant数据结构的内存。

b. 调用wpa_supplicant_init_iface() 函数来做网络接口的初始工作,主要包括:

设置驱动类型,默认是wext;

读取配置文件,并将其中的信息设置到wpa_supplicant数据结构中的conf 指针指向的数据结构,它是一个wpa_config类型;

命令行设置的控制接口ctrl_interface和驱动参数driver_param覆盖配置文件里设置,命令行中的优先;

拷贝网络接口名称和桥接口名称到wpa_config数据结构;

对于网络配置块有两个链表描述它,一个是 config->ssid,它按照配置文件中的顺序依次挂载在这个链表上,还有一个是pssid,它是一个二级指针,指向一个指针数组,该指针数组按照优先级从高到底的顺序依次保存wpa_ssid指针,相同优先级的在同一链表中挂载。

c. 调用wpa_supplicant_init_iface2() 函数,主要包括:

调用wpa_supplicant_init_eapol()函数来初始化eapol;

调用相应类型的driver的init()函数;

设置driver的param参数;

调用wpa_drv_get_ifname()函数获得网络接口的名称,对于wext类型的driver,没有这个接口函数;

调用wpa_supplicant_init_wpa()函数来初始化wpa,并做相应的初始化工作;

调用wpa_supplicant_driver_init()函数,来初始化driver接口参数;在该函数的最后,会

wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;

wpa_supplicant_req_scan(wpa_s, interface_count, 100000);

来主动发起scan,

调用wpa_supplicant_ctrl_iface_init()函数,来初始化控制接口;对于UNIX SOCKET这种方式,其本地socket文件是由配置文件里的ctrl_interface参数指定的路径加上网络接口名称;

 

2.4. wpa_supplicant_run()函数:

初始化完成之后,让wpa_supplicant的main event loop run起来。

在wpa_supplicant中,有许多与外界通信的socket,它们都是需要注册到eloop event模块中的,具体地说,就是在eloop_sock_table中增加一项记录,其中包括了sock_fd, handle, eloop_data, user_data。

eloop event模块就是将这些socket组织起来,统一管理,然后在eloop_run中利用select机制来管理socket的通信。

 

3. Wpa_supplicant提供的接口

从通信层次上划分,wpa_supplicant提供向上的控制接口 control interface,用于与其他模块(如UI)进行通信,其他模块可以通过control interface 来获取信息或下发命令。Wpa_supplicant通过socket通信机制实现下行接口,与内核进行通信,获取信息或下发命令。

 

3.1 上行接口

Wpa_supplicant提供两种方式的上行接口。一种基于传统dbus机制实现与其他进程间的IPC通信;另一种通过Unix domain socket机制实现进程间的IPC通信。

3.1.1 Dbus接口

该接口主要在文件“ctrl_iface_dbus.h”,“ctrl_iface_dbus.c”,“ctrl_iface_dbus_handler.h”和“ctrl_iface_dbus_handler.c”中实现,提供一些基本的控制方法。

 

DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message);

 

DBusMessage * wpas_dbus_global_add_interface(DBusMessage *message,

                                        struct wpa_global *global);

 

DBusMessage * wpas_dbus_global_remove_interface(DBusMessage *message,

                                          struct wpa_global *global);

 

DBusMessage * wpas_dbus_global_get_interface(DBusMessage *message,

                                        struct wpa_global *global);

 

DBusMessage * wpas_dbus_global_set_debugparams(DBusMessage *message,

                                          struct wpa_global *global);

 

DBusMessage * wpas_dbus_iface_scan(DBusMessage *message,

                               struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_scan_results(DBusMessage *message,

                                      struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_bssid_properties(DBusMessage *message,

                                    struct wpa_supplicant *wpa_s,

                                    struct wpa_scan_res *res);

 

DBusMessage * wpas_dbus_iface_capabilities(DBusMessage *message,

                                      struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_add_network(DBusMessage *message,

                                     struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_remove_network(DBusMessage *message,

                                        struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_set_network(DBusMessage *message,

                                     struct wpa_supplicant *wpa_s,

                                     struct wpa_ssid *ssid);

 

DBusMessage * wpas_dbus_iface_enable_network(DBusMessage *message,

                                        struct wpa_supplicant *wpa_s,

                                        struct wpa_ssid *ssid);

 

DBusMessage * wpas_dbus_iface_disable_network(DBusMessage *message,

                                         struct wpa_supplicant *wpa_s,

                                         struct wpa_ssid *ssid);

 

DBusMessage * wpas_dbus_iface_select_network(DBusMessage *message,

                                             struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_disconnect(DBusMessage *message,

                                    struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_set_ap_scan(DBusMessage *message,

                                          struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_set_smartcard_modules(

       DBusMessage *message, struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_get_state(DBusMessage *message,

                                   struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_get_scanning(DBusMessage *message,

                                      struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_set_blobs(DBusMessage *message,

                                    struct wpa_supplicant *wpa_s);

 

DBusMessage * wpas_dbus_iface_remove_blobs(DBusMessage *message,

                                      struct wpa_supplicant *wpa_s);

 

3.1.2 Unix domain socket 接口

该接口主要在文件“wpa_ctrl.h”,“wpa_ctrl.c”,“ctrl_iface_unix.c”,“ctrl_iface.h”和“ctrl_iface.c”实现。

 

(1“wpa_ctrl.h”,“wpa_ctrl.c”完成对control interface的封装,对外提供统一的接口。其主要的工作是通过Unix domain socket建立一个control interface 的client结点,与作为server的wpa_supplicant结点通信。

 

主要功能函数:

struct wpa_ctrl * wpa_ctrl_open(const char *ctrl_path);

/* 建立并初始化一个Unix domain socket的client结点,并与作为server的wpa_supplicant结点绑定 */


void wpa_ctrl_close(struct wpa_ctrl *ctrl);

/* 撤销并销毁已建立的Unix domain socket的client结点 */

 

int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len,

                   char *reply, size_t *reply_len,

                   void (*msg_cb)(char *msg, size_t len));

 

/* 用户模块直接调用该函数对wpa_supplicant发送命令并获取所需信息

 * 可以发送的命令如附件1所示 */

Note:

       Wpa_supplicant 提供两种由外部模块获取信息的方式:一种是外部模块通过发送request 命令然后获取response的问答模式,另一种是wpa_supplicant主动向外部发送event事件,由外部模块监听接收。

 

       一般的常用做法是外部模块通过调用wpa_ctrl_open()两次,建立两个control interface接口,一个为ctrl interface,用于发送命令,获取信息,另一个为monitor interface,用于监听接收来自于wpa_supplicant的event时间。此举可以降低通信的耦合性,避免response和event的相互干扰。

 

int wpa_ctrl_attach(struct wpa_ctrl *ctrl);

/* 注册 某个 control interface 作为 monitor interface */

 

int wpa_ctrl_detach(struct wpa_ctrl *ctrl);

/* 撤销某个 monitor interface 为 普通的 control interface  */

 

int wpa_ctrl_pending(struct wpa_ctrl *ctrl);

/* 判断是否有挂起的event 事件 */

 

int wpa_ctrl_recv(struct wpa_ctrl *ctrl, char *reply, size_t *reply_len);

/* 获取挂起的event 事件 */

 

(2“ctrl_iface_unix.c”实现wpa_supplicant的Unix domain socket通信机制中server结点,完成对client结点的响应。

       其中最主要的两个函数为:

static void wpa_supplicant_ctrl_iface_receive(int sock, void *eloop_ctx,

                                         void *sock_ctx)

/* 接收并解析client发送request命令,然后根据不同的命令调用底层不同的处理函数;

 * 然后将获得response结果回馈到 client 结点。

 */

 

static void wpa_supplicant_ctrl_iface_send(struct ctrl_iface_priv *priv,

                                      int level, const char *buf,

                                      size_t len)

/* 向注册的monitor interfaces 主动发送event事件 */

 

(3“ctrl_iface.h”和“ctrl_iface.c”主要实现了各种request命令的底层处理函数。

 

3.2 下行接口

Wpa_supplicant提供的下行接口主要用于和kernel(driver)进行通信,下发命令和获取信息。

Wpa_supplicant下行接口主要包括三种重要的接口:

1.    PF_INET socket接口,主要用于向kernel 发送ioctl命令,控制并获取相应信息。

2.    PF_NETLINK socket接口,主要用于接收kernel发送上来的event 事件。

3.    PF_PACKET socket接口,主要用于向driver传递802.1X报文。

 

主要涉及到的文件包括:“driver.h”,“drivers.c”,“driver_wext.h”,“driver_wext.c”,“l2_packet.h”和“l2_packet_linux.c”。其中“driver.h”,“drivers.c”,“driver_wext.h”和“driver_wext.c”实现PF_INET socket接口和PF_NETLINK socket接口;“l2_packet.h”和“l2_packet_linux.c”实现PF_PACKET socket接口。

 

(1“driver.h”,“drivers.c”主要用于封装底层差异对外显示一个相同的wpa_driver_ops接口。Wpa_supplicant可支持atmel, Broadcom, ipw, madwifi, ndis, nl80211, wext等多种驱动。

其中一个最主要的数据结构为wpa_driver_ops, 其定义了driver相关的各种操作接口。

 

(2“driver_wext.h”,“driver_wext.c”实现了wext形式的wpa_driver_ops并创建了PF_INET socket接口和PF_NETLINK socket接口,然后通过这两个接口完成与kernel的信息交互。

 

Wext提供的一个主要数据结构为:

struct wpa_driver_wext_data {

       void *ctx;

       int event_sock;

       int ioctl_sock;

       int mlme_sock;

       char ifname[IFNAMSIZ + 1];

       int ifindex;

       int ifindex2;

       int if_removed;

       u8 *assoc_req_ies;

       size_t assoc_req_ies_len;

       u8 *assoc_resp_ies;

       size_t assoc_resp_ies_len;

       struct wpa_driver_capa capa;

       int has_capability;

       int we_version_compiled;

 

       /* for set_auth_alg fallback */

       int use_crypt;

       int auth_alg_fallback;

 

       int operstate;

 

       char mlmedev[IFNAMSIZ + 1];

 

       int scan_complete_events;

};

其中event_sock 为PF_NETLINK socket接口,ioctl_sock为PF_INET socket借口。

 

Driver_wext.c实现了大量底层处理函数用于实现wpa_driver_ops操作参数,其中比较重要的有:

void * wpa_driver_wext_init(void *ctx, const char *ifname);

/* 初始化wpa_driver_wext_data 数据结构,并创建PF_NETLINK socket和 PF_INET socket 接口 */

 

void wpa_driver_wext_deinit(void *priv);

/* 销毁wpa_driver_wext_data 数据结构,PF_NETLINK socket和 PF_INET socket 接口 */

 

static void wpa_driver_wext_event_receive(int sock, void *eloop_ctx,

                                     void *sock_ctx);

/* 处理kernel主动发送的event事件的 callback 函数 */

 

最后,将实现的操作函数映射到一个全局的wpa_driver_ops类型数据结构 wpa_driver_wext_ops中。

 

const struct wpa_driver_ops wpa_driver_wext_ops = {

       .name = "wext",

       .desc = "Linux wireless extensions (generic)",

       .get_bssid = wpa_driver_wext_get_bssid,

       .get_ssid = wpa_driver_wext_get_ssid,

       .set_wpa = wpa_driver_wext_set_wpa,

       .set_key = wpa_driver_wext_set_key,

       .set_countermeasures = wpa_driver_wext_set_countermeasures,

       .set_drop_unencrypted = wpa_driver_wext_set_drop_unencrypted,

       .scan = wpa_driver_wext_scan,

       .get_scan_results2 = wpa_driver_wext_get_scan_results,

       .deauthenticate = wpa_driver_wext_deauthenticate,

       .disassociate = wpa_driver_wext_disassociate,

       .set_mode = wpa_driver_wext_set_mode,

       .associate = wpa_driver_wext_associate,

       .set_auth_alg = wpa_driver_wext_set_auth_alg,

       .init = wpa_driver_wext_init,

       .deinit = wpa_driver_wext_deinit,

       .add_pmkid = wpa_driver_wext_add_pmkid,

       .remove_pmkid = wpa_driver_wext_remove_pmkid,

       .flush_pmkid = wpa_driver_wext_flush_pmkid,

       .get_capa = wpa_driver_wext_get_capa,

       .set_operstate = wpa_driver_wext_set_operstate,

};

 

(3“l2_packet.h”和“l2_packet_linux.c”主要用于实现PF_PACKET socket接口,通过该接口,wpa_supplicant可以直接将802.1X packet发送到L2层,而不经过TCP/IP协议栈。

 

其中主要的功能函数为:

struct l2_packet_data * l2_packet_init(

       const char *ifname, const u8 *own_addr, unsigned short protocol,

       void (*rx_callback)(void *ctx, const u8 *src_addr,

                         const u8 *buf, size_t len),

       void *rx_callback_ctx, int l2_hdr);

/* 创建并初始化PF_PACKET socket接口,其中rx_callback 为从L2接收到的packet 处理callback函数 */

 

void l2_packet_deinit(struct l2_packet_data *l2);

/* 销毁 PF_PACKET socket接口 */

 

int l2_packet_send(struct l2_packet_data *l2, const u8 *dst_addr, u16 proto,

                 const u8 *buf, size_t len);

/* L2层packet发送函数,wpa_supplicant用此发送L2层 802.1X packet  */

 

static void l2_packet_receive(int sock, void *eloop_ctx, void *sock_ctx);

/*  L2层packet接收函数,接收来自L2层数据后,将其发送到上层  */

4. Control interface commands

       PING

       MIB

       STATUS

       STATUS-VERBOSE

       PMKSA

       SET <variable> <valus>

       LOGON

       LOGOFF

       REASSOCIATE

       RECONNECT

       PREAUTH <BSSID>

       ATTACH

       DETACH

       LEVEL <debug level>

       RECONFIGURE

       TERMINATE

       BSSID <network id> <BSSID>

       LIST_NETWORKS

       DISCONNECT

       SCAN

       SCAN_RESULTS

       BSS

       SELECT_NETWORK <network id>

       ENABLE_NETWORK <network id>

       DISABLE_NETWORK <network id>

       ADD_NETWORK

       REMOVE_NETWORK <network id>

       SET_NETWORK <network id> <variable> <value>

       GET_NETWORK <network id> <variable>

       SAVE_CONFIG


----------------------------------------

Linux无线网络设置(wpa_supplicant的使用)

主机环境:Gentoo Linux 3.1.10
 WPA Supplicant工具包可以让您连接到那些使用WPA的AP。因为还只是beta版,所以它的配置方法仍会常常变化——尽管如此,在大部分情况下它已经能很好的工作。
 安装上wap_supplicant后可以通过修改/etc/wpa_supplicant/wpa_supplicant.conf来进行配置无线接入点网络
 下面是一个配置文件的实例。
 
 # 请不要修改下面这一行内容,否则将不能正常工作
 ctrl_interface=/var/run/wpa_supplicant
 
 # 确保只有root用户能读取WPA的配置
 ctrl_interface_group=0
 
 # 使用wpa_supplicant来扫描和选择AP
 ap_scan=1
 
 # 简单的情形:WPA-PSk密码验证方式,PSK是ASCII密码短语,所有合法的加密方式都允许连接
 network={
 ssid="simple"
 psk="very secret passphrase"
 # 优先级越高,就能越早匹配到。
 priority=5
 }
 
 # 与前面的设置相同,但要求对特定的SSID进行扫描(针对那些拒绝广播SSID的AP)
 network={
 ssid="second ssid"
 scan_ssid=1
 psk="very secret passphrase"
 priority=2
 }
 
 # 仅使用WPA-PSK方式。允许使用任何合法的加密方式的组合
 network={
 ssid="example"
 proto=WPA
 key_mgmt=WPA-PSK
 pairwise=CCMP TKIP
 group=CCMP TKIP WEP104 WEP40
 psk=06b4be19da289f475aa46a33cb793029d4ab3db7a23ee92382eb0106c72ac7bb
 priority=2
 }
 
 # 明文连接方式(不使用WPA和IEEE802.1X)
 network={
 ssid="plaintext-test"
 key_mgmt=NONE
 }
 
 # 共享WEP秘钥连接方式(不使用WPA和IEEE802.1X)
 network={
 ssid="static-wep-test"
 key_mgmt=NONE
 wep_key0="abcde"
 wep_key1=0102030405
 wep_key2="1234567890123"
 wep_tx_keyidx=0
 priority=5
 }
 
 # 共享WEP秘钥连接方式(无WPA和IEEE802.1X),使用共享秘钥IEEE802.11验证方式
 network={
 ssid="static-wep-test2"
 key_mgmt=NONE
 wep_key0="abcde"
 wep_key1=0102030405
 wep_key2="1234567890123"
 wep_tx_keyidx=0
 priority=5
 auth_alg=SHARED
 }
 
 # 在IBSS/ad-hoc网络中使用WPA-None/TKIP
 network={
 ssid="test adhoc"
 mode=1
 proto=WPA
 key_mgmt=WPA-NONE
 pairwise=NONE
 group=TKIP
 psk="secret passphrase"
 }
 
 --
 下面是我的配置文件
 
 ctrl_interface=/var/run/wpa_supplicant
 ap_scan=1
 
 #Home Network
 network={
     psk="yming0221"
     priority=1
     ssid=79616E277320776972656C657373
     mode=0
     bssid=E0:05:C5:17:F8:2C
     key_mgmt=WPA-PSK
 }
 #
 network={
     ssid="351471azjlb"
     psk="CCTV1-CCTV2-KTV-1987"
     priority=2
 }
 
 然后重启wlan0连接

 /etc/init.d/net.wlan0 restart



======================================================================================

常用命令:

wpa_supplicant -Dwext -iwlan0 -c配置文件.conf -C/var/run/wpa_supplicant -B

-B: 后台运行

-c: 配置文件

-C:unix socket 名称

-i:监听的接口

-D:使用的驱动名, 一般为wext或者 nl80211



wpa_passphrase
              创建 wpa_supplicant.conf 的工具

        wpa_passphrase [ ssid ] [ passphrase ]  > conf 文件


wpa_cli

    wpa_cli  [  -p path to ctrl sockets ] [ -i ifname ] [ -hvB ] [ -a action file ] [ -P pid file ] [command ... ]

    wpa_cli -i wlan0     |

                                | list_network

                                | remove_netwok

                                | add_network

                                | set_network %d    | ssid "名称"

                                                              | key_mgmt 类型(NONE, )

                                                              | wep_key0 密码

                                                              | psk 密码

                                                              | wep_tx_keyidx 0

                                | select_network %d

                                | enable_network %d

                                | save_config

                                | scan

                                | scan_results

                                | terminate


wpa_cli用法

1: run wpa_supplicant first

use the following command:

       wpa_supplicant -Dwext -iwlan0 -C/data/system/wpa_supplicant -c/data/misc/wifi/wpa_supplicant.conf

      (use “ps”to make sure wpa_supplicant is running )

 

2: Run the command line tool wpa_cli to connect wifi

       wpa_cli -p/data/system/wpa_supplicant -iwlan0

       Then , it will let you set network interactively

 

       some common command:

       >scan = to scan the neighboring AP

       >scan_results = show the scan results

       >status = check out the current connection information

       >terminate = terminate wpa_supplicant

       >quit = exit wpa_cli

       >add_network = it will return a network id to you

       >set_network <network id> <variable> <value> = set network variables (shows

list of variables when run without arguments), success will return OK, or will return Fail

       >select_network <network id> = select a network (disable others)

       >disable_network <network id> = disable a network

       >enable_network <network id> = enable a network

 

3: example

 

       for AP that doesn`t have encryption

              >add_network      (It will display a network id for you, assume it returns 0)

              >set_network 0 ssid “666”

              >set_network 0 key_mgmt NONE

              >enable_network 0

              >quit

       if normal, we have connectted to the AP “666”, now you need a IP to access internet, for example:

              dhcpcd wlan0

              if everything is ok, it will get an IP & can access internet

 

       for AP that has WEP

              >add_network      (assume returns 1)

              >set_network 1 ssid “666”

              >set_network 1 key_mgmt NONE

              >set_network 1 wep_key0 “your ap passwork”(if usting ASCII, it need double quotation marks, if using hex, then don`t need the double quotation marks)

              >set_network 1 wep_tx_keyidx 0

              >select_network 1  (optional, remember, if you are connecting with another AP, you should select it to disable the another)

              >enable_network 1

              and then ,get an IP to access internet

 

       for AP that has WPA-PSK/WPA2-PSK

              >add_network      (assume returns 2)

              >set_network 2 ssid “666”

              >set_network 2 psk “your pre-shared key”

              >select_network 2  (optional, remember, if you are connecting with another AP, you should select it to disable the another)

              >enable_network 2

              there is still some others options to be set, but wpa_supplicant will choose the default for you, the default will include all we need to set

              and then ,get an IP to access internet

 

       for Hidden AP(补充)

        原则上应该只要在上面的基础上去set_network netid scan_ssid 1即可,测试过无加密的Hidden AP,WEP/WPA/WPA2应该道理一样

=====================  wpa_supplicant.conf 官方描述(其中包含了 set_network 子命令中所带的参数与取值范围) ========================================

##### Example wpa_supplicant configuration file ###############################
#
# This file describes configuration file format and lists all available option.
# Please also take a look at simpler configuration examples in ‘examples‘
# subdirectory.
#
# Empty lines and lines starting with # are ignored

# NOTE! This file may contain password information and should probably be made
# readable only by root user on multiuser systems.

# Note: All file paths in this configuration file should use full (absolute,
# not relative to working directory) path in order to allow working directory
# to be changed. This can happen if wpa_supplicant is run in the background.

# Whether to allow wpa_supplicant to update (overwrite) configuration
#
# This option can be used to allow wpa_supplicant to overwrite configuration
# file whenever configuration is changed (e.g., new network block is added with
# wpa_cli or wpa_gui, or a password is changed). This is required for
# wpa_cli/wpa_gui to be able to store the configuration changes permanently.
# Please note that overwriting configuration file will remove the comments from
# it.
#update_config=1

# global configuration (shared by all network blocks)
#
# Parameters for the control interface. If this is specified, wpa_supplicant
# will open a control interface that is available for external programs to
# manage wpa_supplicant. The meaning of this string depends on which control
# interface mechanism is used. For all cases, the existance of this parameter
# in configuration is used to determine whether the control interface is
# enabled.
#
# For UNIX domain sockets (default on Linux and BSD): This is a directory that
# will be created for UNIX domain sockets for listening to requests from
# external programs (CLI/GUI, etc.) for status information and configuration.
# The socket file will be named based on the interface name, so multiple
# wpa_supplicant processes can be run at the same time if more than one
# interface is used.
# /var/run/wpa_supplicant is the recommended directory for sockets and by
# default, wpa_cli will use it when trying to connect with wpa_supplicant.
#
# Access control for the control interface can be configured by setting the
# directory to allow only members of a group to use sockets. This way, it is
# possible to run wpa_supplicant as root (since it needs to change network
# configuration and open raw sockets) and still allow GUI/CLI components to be
# run as non-root users. However, since the control interface can be used to
# change the network configuration, this access needs to be protected in many
# cases. By default, wpa_supplicant is configured to use gid 0 (root). If you
# want to allow non-root users to use the control interface, add a new group
# and change this value to match with that group. Add users that should have
# control interface access to this group. If this variable is commented out or
# not included in the configuration file, group will not be changed from the
# value it got by default when the directory or socket was created.
#
# When configuring both the directory and group, use following format:
# DIR=/var/run/wpa_supplicant GROUP=wheel
# DIR=/var/run/wpa_supplicant GROUP=0
# (group can be either group name or gid)
#
# For UDP connections (default on Windows): The value will be ignored. This
# variable is just used to select that the control interface is to be created.
# The value can be set to, e.g., udp (ctrl_interface=udp)
#
# For Windows Named Pipe: This value can be used to set the security descriptor
# for controlling access to the control interface. Security descriptor can be
# set using Security Descriptor String Format (see http://msdn.microsoft.com/
# library/default.asp?url=/library/en-us/secauthz/security/
# security_descriptor_string_format.asp). The descriptor string needs to be
# prefixed with SDDL=. For example, ctrl_interface=SDDL=D: would set an empty
# DACL (which will reject all connections). See README-Windows.txt for more
# information about SDDL string format.
#
ctrl_interface=/var/run/wpa_supplicant

# IEEE 802.1X/EAPOL version
# wpa_supplicant is implemented based on IEEE Std 802.1X-2004 which defines
# EAPOL version 2. However, there are many APs that do not handle the new
# version number correctly (they seem to drop the frames completely). In order
# to make wpa_supplicant interoperate with these APs, the version number is set
# to 1 by default. This configuration value can be used to set it to the new
# version (2).
eapol_version=1

# AP scanning/selection
# By default, wpa_supplicant requests driver to perform AP scanning and then
# uses the scan results to select a suitable AP. Another alternative is to
# allow the driver to take care of AP scanning and selection and use
# wpa_supplicant just to process EAPOL frames based on IEEE 802.11 association
# information from the driver.
# 1: wpa_supplicant initiates scanning and AP selection
# 0: driver takes care of scanning, AP selection, and IEEE 802.11 association
#    parameters (e.g., WPA IE generation); this mode can also be used with
#    non-WPA drivers when using IEEE 802.1X mode; do not try to associate with
#    APs (i.e., external program needs to control association). This mode must
#    also be used when using wired Ethernet drivers.
# 2: like 0, but associate with APs using security policy and SSID (but not
#    BSSID); this can be used, e.g., with ndiswrapper and NDIS drivers to
#    enable operation with hidden SSIDs and optimized roaming; in this mode,
#    the network blocks in the configuration file are tried one by one until
#    the driver reports successful association; each network block should have
#    explicit security policy (i.e., only one option in the lists) for
#    key_mgmt, pairwise, group, proto variables
ap_scan=1

# EAP fast re-authentication
# By default, fast re-authentication is enabled for all EAP methods that
# support it. This variable can be used to disable fast re-authentication.
# Normally, there is no need to disable this.
fast_reauth=1

# OpenSSL Engine support
# These options can be used to load OpenSSL engines.
# The two engines that are supported currently are shown below:
# They are both from the opensc project (http://www.opensc.org/)
# By default no engines are loaded.
# make the opensc engine available
#opensc_engine_path=/usr/lib/opensc/engine_opensc.so
# make the pkcs11 engine available
#pkcs11_engine_path=/usr/lib/opensc/engine_pkcs11.so
# configure the path to the pkcs11 module required by the pkcs11 engine
#pkcs11_module_path=/usr/lib/pkcs11/opensc-pkcs11.so

# Dynamic EAP methods
# If EAP methods were built dynamically as shared object files, they need to be
# loaded here before being used in the network blocks. By default, EAP methods
# are included statically in the build, so these lines are not needed
#load_dynamic_eap=/usr/lib/wpa_supplicant/eap_tls.so
#load_dynamic_eap=/usr/lib/wpa_supplicant/eap_md5.so

# Driver interface parameters
# This field can be used to configure arbitrary driver interace parameters. The
# format is specific to the selected driver interface. This field is not used
# in most cases.
#driver_param="field=value"

# Country code
# The ISO/IEC alpha2 country code for the country in which this device is
# currently operating.
#country=US

# Maximum lifetime for PMKSA in seconds; default 43200
#dot11RSNAConfigPMKLifetime=43200
# Threshold for reauthentication (percentage of PMK lifetime); default 70
#dot11RSNAConfigPMKReauthThreshold=70
# Timeout for security association negotiation in seconds; default 60
#dot11RSNAConfigSATimeout=60

# Wi-Fi Protected Setup (WPS) parameters

# Universally Unique IDentifier (UUID; see RFC 4122) of the device
# If not configured, UUID will be generated based on the local MAC address.
#uuid=12345678-9abc-def0-1234-56789abcdef0

# Device Name
# User-friendly description of device; up to 32 octets encoded in UTF-8
#device_name=Wireless Client

# Manufacturer
# The manufacturer of the device (up to 64 ASCII characters)
#manufacturer=Company

# Model Name
# Model of the device (up to 32 ASCII characters)
#model_name=cmodel

# Model Number
# Additional device description (up to 32 ASCII characters)
#model_number=123

# Serial Number
# Serial number of the device (up to 32 characters)
#serial_number=12345

# Primary Device Type
# Used format: <categ>-<OUI>-<subcateg>
# categ = Category as an integer value
# OUI = OUI and type octet as a 4-octet hex-encoded value; 0050F204 for
#       default WPS OUI
# subcateg = OUI-specific Sub Category as an integer value
# Examples:
#   1-0050F204-1 (Computer / PC)
#   1-0050F204-2 (Computer / Server)
#   5-0050F204-1 (Storage / NAS)
#   6-0050F204-1 (Network Infrastructure / AP)
#device_type=1-0050F204-1

# OS Version
# 4-octet operating system version number (hex string)
#os_version=01020300

# Credential processing
#   0 = process received credentials internally (default)
#   1 = do not process received credentials; just pass them over ctrl_iface to
#    external program(s)
#   2 = process received credentials internally and pass them over ctrl_iface
#    to external program(s)
#wps_cred_processing=0

# network block
#
# Each network (usually AP‘s sharing the same SSID) is configured as a separate
# block in this configuration file. The network blocks are in preference order
# (the first match is used).
#
# network block fields:
#
# disabled:
#    0 = this network can be used (default)
#    1 = this network block is disabled (can be enabled through ctrl_iface,
#        e.g., with wpa_cli or wpa_gui)
#
# id_str: Network identifier string for external scripts. This value is passed
#    to external action script through wpa_cli as WPA_ID_STR environment
#    variable to make it easier to do network specific configuration.
#
# ssid: SSID (mandatory); either as an ASCII string with double quotation or
#    as hex string; network name
#
# scan_ssid:
#    0 = do not scan this SSID with specific Probe Request frames (default)
#    1 = scan with SSID-specific Probe Request frames (this can be used to
#        find APs that do not accept broadcast SSID or use multiple SSIDs;
#        this will add latency to scanning, so enable this only when needed)
#
# bssid: BSSID (optional); if set, this network block is used only when
#    associating with the AP using the configured BSSID
#
# priority: priority group (integer)
# By default, all networks will get same priority group (0). If some of the
# networks are more desirable, this field can be used to change the order in
# which wpa_supplicant goes through the networks when selecting a BSS. The
# priority groups will be iterated in decreasing priority (i.e., the larger the
# priority value, the sooner the network is matched against the scan results).
# Within each priority group, networks will be selected based on security
# policy, signal strength, etc.
# Please note that AP scanning with scan_ssid=1 and ap_scan=2 mode are not
# using this priority to select the order for scanning. Instead, they try the
# networks in the order that used in the configuration file.
#
# mode: IEEE 802.11 operation mode
# 0 = infrastructure (Managed) mode, i.e., associate with an AP (default)
# 1 = IBSS (ad-hoc, peer-to-peer)
# Note: IBSS can only be used with key_mgmt NONE (plaintext and static WEP)
# and key_mgmt=WPA-NONE (fixed group key TKIP/CCMP). In addition, ap_scan has
# to be set to 2 for IBSS. WPA-None requires following network block options:
# proto=WPA, key_mgmt=WPA-NONE, pairwise=NONE, group=TKIP (or CCMP, but not
# both), and psk must also be set.
#
# frequency: Channel frequency in megahertz (MHz) for IBSS, e.g.,
# 2412 = IEEE 802.11b/g channel 1. This value is used to configure the initial
# channel for IBSS (adhoc) networks. It is ignored in the infrastructure mode.
# In addition, this value is only used by the station that creates the IBSS. If
# an IBSS network with the configured SSID is already present, the frequency of
# the network will be used instead of this configured value.
#
# proto: list of accepted protocols
# WPA = WPA/IEEE 802.11i/D3.0
# RSN = WPA2/IEEE 802.11i (also WPA2 can be used as an alias for RSN)
# If not set, this defaults to: WPA RSN
#
# key_mgmt: list of accepted authenticated key management protocols
# WPA-PSK = WPA pre-shared key (this requires ‘psk‘ field)
# WPA-EAP = WPA using EAP authentication
# IEEE8021X = IEEE 802.1X using EAP authentication and (optionally) dynamically
#    generated WEP keys
# NONE = WPA is not used; plaintext or static WEP could be used
# WPA-PSK-SHA256 = Like WPA-PSK but using stronger SHA256-based algorithms
# WPA-EAP-SHA256 = Like WPA-EAP but using stronger SHA256-based algorithms
# If not set, this defaults to: WPA-PSK WPA-EAP
#
# auth_alg: list of allowed IEEE 802.11 authentication algorithms
# OPEN = Open System authentication (required for WPA/WPA2)
# SHARED = Shared Key authentication (requires static WEP keys)
# LEAP = LEAP/Network EAP (only used with LEAP)
# If not set, automatic selection is used (Open System with LEAP enabled if
# LEAP is allowed as one of the EAP methods).
#
# pairwise: list of accepted pairwise (unicast) ciphers for WPA
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
# NONE = Use only Group Keys (deprecated, should not be included if APs support
#    pairwise keys)
# If not set, this defaults to: CCMP TKIP
#
# group: list of accepted group (broadcast/multicast) ciphers for WPA
# CCMP = AES in Counter mode with CBC-MAC [RFC 3610, IEEE 802.11i/D7.0]
# TKIP = Temporal Key Integrity Protocol [IEEE 802.11i/D7.0]
# WEP104 = WEP (Wired Equivalent Privacy) with 104-bit key
# WEP40 = WEP (Wired Equivalent Privacy) with 40-bit key [IEEE 802.11]
# If not set, this defaults to: CCMP TKIP WEP104 WEP40
#
# psk: WPA preshared key; 256-bit pre-shared key
# The key used in WPA-PSK mode can be entered either as 64 hex-digits, i.e.,
# 32 bytes or as an ASCII passphrase (in which case, the real PSK will be
# generated using the passphrase and SSID). ASCII passphrase must be between
# 8 and 63 characters (inclusive).
# This field is not needed, if WPA-EAP is used.
# Note: Separate tool, wpa_passphrase, can be used to generate 256-bit keys
# from ASCII passphrase. This process uses lot of CPU and wpa_supplicant
# startup and reconfiguration time can be optimized by generating the PSK only
# only when the passphrase or SSID has actually changed.
#
# eapol_flags: IEEE 802.1X/EAPOL options (bit field)
# Dynamic WEP key required for non-WPA mode
# bit0 (1): require dynamically generated unicast WEP key
# bit1 (2): require dynamically generated broadcast WEP key
#     (3 = require both keys; default)
# Note: When using wired authentication, eapol_flags must be set to 0 for the
# authentication to be completed successfully.
#
# mixed_cell: This option can be used to configure whether so called mixed
# cells, i.e., networks that use both plaintext and encryption in the same
# SSID, are allowed when selecting a BSS form scan results.
# 0 = disabled (default)
# 1 = enabled
#
# proactive_key_caching:
# Enable/disable opportunistic PMKSA caching for WPA2.
# 0 = disabled (default)
# 1 = enabled
#
# wep_key0..3: Static WEP key (ASCII in double quotation, e.g. "abcde" or
# hex without quotation, e.g., 0102030405)
# wep_tx_keyidx: Default WEP key index (TX) (0..3)
#
# peerkey: Whether PeerKey negotiation for direct links (IEEE 802.11e DLS) is
# allowed. This is only used with RSN/WPA2.
# 0 = disabled (default)
# 1 = enabled
#peerkey=1
#
# wpa_ptk_rekey: Maximum lifetime for PTK in seconds. This can be used to
# enforce rekeying of PTK to mitigate some attacks against TKIP deficiencies.
#
# Following fields are only used with internal EAP implementation.
# eap: space-separated list of accepted EAP methods
#    MD5 = EAP-MD5 (unsecure and does not generate keying material ->
#            cannot be used with WPA; to be used as a Phase 2 method
#            with EAP-PEAP or EAP-TTLS)
#       MSCHAPV2 = EAP-MSCHAPv2 (cannot be used separately with WPA; to be used
#        as a Phase 2 method with EAP-PEAP or EAP-TTLS)
#       OTP = EAP-OTP (cannot be used separately with WPA; to be used
#        as a Phase 2 method with EAP-PEAP or EAP-TTLS)
#       GTC = EAP-GTC (cannot be used separately with WPA; to be used
#        as a Phase 2 method with EAP-PEAP or EAP-TTLS)
#    TLS = EAP-TLS (client and server certificate)
#    PEAP = EAP-PEAP (with tunnelled EAP authentication)
#    TTLS = EAP-TTLS (with tunnelled EAP or PAP/CHAP/MSCHAP/MSCHAPV2
#             authentication)
#    If not set, all compiled in methods are allowed.
#
# identity: Identity string for EAP
#    This field is also used to configure user NAI for
#    EAP-PSK/PAX/SAKE/GPSK.
# anonymous_identity: Anonymous identity string for EAP (to be used as the
#    unencrypted identity with EAP types that support different tunnelled
#    identity, e.g., EAP-

以上是关于wpa_supplicant软件架构分析的主要内容,如果未能解决你的问题,请参考以下文章

Android wifi 扫描流程 分析

Android框架WiFi架构

Android wpa_supplicant 四次握手 流程分析

hostapd wpa_supplicant madwifi详细分析——wpa_supplicant的配置文件

hostapd wpa_supplicant madwifi详细分析——wps原理及实现 一

hostapd wpa_supplicant madwifi详细分析——hostapd整体梳理