以下分析基于 wpa_supplicant 0.5.11 版本
1、wpa_supplicant簡介
wpa_supplicant is an implementation of the WPA Supplicant component,
i.e., the part that runs in the client stations. It implements WPA key
negotiation with a WPA Authenticator and EAP authentication with
Authentication Server. In addition, it controls the roaming and IEEE
802.11 authentication/association of the wlan driver.
wpa_supplicant is designed to be a "daemon" program that runs in the
background and acts as the backend component controlling the wireless
connection. wpa_supplicant supports separate frontend programs and an
example text-based frontend, wpa_cli, is included with wpa_supplicant.
Following steps are used when associating with an AP using WPA:
- wpa_supplicant requests the kernel driver to scan neighboring BSSes
- wpa_supplicant selects a BSS based on its configuration
- wpa_supplicant requests the kernel driver to associate with the chosen BSS
- If WPA-EAP: integrated IEEE 802.1X Supplicant completes EAP
authentication with the authentication server (proxied by theAuthenticator in the AP)
- If WPA-EAP: master key is received from the IEEE 802.1X Supplicant
- If WPA-PSK: wpa_supplicant uses PSK as the master session key
- wpa_supplicant completes WPA 4-Way Handshake and Group Key Handshake
with the Authenticator (AP)
- wpa_supplicant configures encryption keys for unicast and broadcast
- normal data packets can be transmitted and received
2、啟動命令
You will need to make a configuration file, e.g.,
/etc/wpa_supplicant.conf, with network configuration for the networks
you are going to use. Configuration file section below includes
explanation fo the configuration file format and includes various
examples. Once the configuration is ready, you can test whether the
configuration work by first running wpa_supplicant with following
command to start it on foreground with debugging enabled:
wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -d
Assuming everything goes fine, you can start using following command
to start wpa_supplicant on background without debugging:
wpa_supplicant -iwlan0 -c/etc/wpa_supplicant.conf -B
Please note that if you included more than one driver interface in the
build time configuration (.config), you may need to specify which
interface to use by including -D<driver name> option on the command??
line. See following section for more details on command line options
for wpa_supplicant.
Command line options
--------------------
usage:
wpa_supplicant [-BddfhKLqqtuvwW] [-P<pid file>] [-g<global ctrl>] \
-i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] [-p<driver_param>] \
[-b<br_ifname> [-N -i<ifname> -c<conf> [-C<ctrl>] [-D<driver>] \
[-p<driver_param>] [-b<br_ifname>] ...]
options:
-b = optional bridge interface name
-B = run daemon in the background
-c = Configuration file
-C = ctrl_interface parameter (only used if -c is not)
-i = interface name
-d = increase debugging verbosity (-dd even more)
-D = driver name
-f = Log output to default log location (normally /tmp)
-g = global ctrl_interface
-K = include keys (passwords, etc.) in debug output
-t = include timestamp in debug messages
-h = show this help text
-L = show license (GPL and BSD)
-p = driver parameters
-P = PID file
-q = decrease debugging verbosity (-qq even less)
-v = show version
-w = wait for interface to be added, if needed
-W = wait for a control interface monitor before starting
-N = start describing new interface
drivers:
hostap = Host AP driver (Intersil Prism2/2.5/3) [default]
(this can also be used with Linuxant DriverLoader)
hermes = Agere Systems Inc. driver (Hermes-I/Hermes-II)
madwifi = MADWIFI 802.11 support (Atheros, etc.)
atmel = ATMEL AT76C5XXx (USB, PCMCIA)
wext = Linux wireless extensions (generic)
ndiswrapper = Linux ndiswrapper
broadcom = Broadcom wl.o driver
ipw = Intel ipw2100/2200 driver (old; use wext with Linux 2.6.13 or newer)
wired = wpa_supplicant wired Ethernet driver
bsd = BSD 802.11 support (Atheros, etc.)
ndis = Windows NDIS driver
In most common cases, wpa_supplicant is started with
wpa_supplicant -Bw -c/etc/wpa_supplicant.conf -iwlan0
This makes the process fork into background and wait for the wlan0
interface if it is not available at startup time.
The easiest way to debug problems, and to get debug log for bug
reports, is to start wpa_supplicant on foreground with debugging
enabled:
wpa_supplicant -c/etc/wpa_supplicant.conf -iwlan0 -d
wpa_supplicant can control multiple interfaces (radios) either by
running one process for each interface separately or by running just
one process and list of options at command line. Each interface is
separated with -N argument. As an example, following command would
start wpa_supplicant for two interfaces:
wpa_supplicant \
-c wpa1.conf -i wlan0 -D hostap -N \
-c wpa2.conf -i ath0 -D madwifi
If the interface is added in a Linux bridge (e.g., br0), the bridge
interface needs to be configured to wpa_supplicant in addition to the
main interface:
wpa_supplicant -cw.conf -Dmadwifi -iath0 -bbr0
3、結(jié)構(gòu)體介紹
struct wpa_interface struct wpa_params struct wpa_global
struct wpa_interface - Parameters for wpa_supplicant_add_iface().
struct wpa_global - Internal, global data for all %wpa_supplicant interfaces.
This structure is initialized by calling wpa_supplicant_init() when starting %wpa_supplicant.
struct wpa_params - Parameters for wpa_supplicant_init().
wpa_params主要記錄一些與網(wǎng)卡本身沒關的參數(shù)設置,而wpa_interface對應網(wǎng)絡接口,
因為wpa_supplicant支持多個網(wǎng)絡接口,所以可能有多個wpa_interface結(jié)構(gòu)體,可以通過命令行
指定不同的接口,wpa_supplicant在main函數(shù)開始的地方會進行遍歷!(參考代碼main.c)
struct wpa_global {
struct wpa_supplicant *ifaces;
struct wpa_params params;
struct ctrl_iface_global_priv *ctrl_iface;
struct ctrl_iface_dbus_priv *dbus_ctrl_iface;
};
struct wpa_supplicant - Internal data for wpa_supplicant interface.
每個網(wǎng)絡接口都有一個對應的wpa_supplicant數(shù)據(jù)結(jié)構(gòu),該指針指向最近加入的一個,在wpa_supplicant數(shù)據(jù)結(jié)構(gòu)中有指針指向next
struct ctrl_iface_global_priv - Global control interface
struct ctrl_iface_dbus_priv - DBUS control interface
4、理解main.c
在這個函數(shù)中,主要做了四件事。
a. android平臺進程wpa_supplicant權(quán)限及結(jié)構(gòu)體初始化,解析命令行參數(shù)
b. 調(diào)用wpa_supplicant_init()函數(shù),主要初始化struct wpa_global *global這個局部結(jié)構(gòu)體同時傳遞給static struct eloop_data eloop這個全局結(jié)構(gòu)體并返回這個局部結(jié)構(gòu)體(詳解見下)
c. for循環(huán)中調(diào)用wpa_supplicant_add_iface()函數(shù),注冊可能有的一個或多個網(wǎng)卡接口
d. 調(diào)用wpa_supplicant_run()函數(shù),這其中如果失敗將會走到out或者是通過goto跳轉(zhuǎn)到out.
下面詳細介紹這四個過程:
4.1 初步初始化及解析命令行參數(shù)
最開始調(diào)用了os_program_init函數(shù),來給wpa_supplicant進程分配權(quán)限,這里的進程一般是在init.rc中有個service,然后通過在wifi.c中的函數(shù)由UI settings設置通過jni調(diào)用下來從而會把service調(diào)用起來!跟蹤這個函數(shù)到達了os_unix.c,見到了我們熟悉的setuid函數(shù)給它設為AID_WIFI(user / group).
然后下面初始化兩大結(jié)構(gòu)體wpa_params和wpa_interface,再然后就開始解析命令行的參數(shù)了,這里不再詳述了
4.2 wpa_supplicant_init(¶ms)
我們先看下這個函數(shù)的說明:
/**
* wpa_supplicant_init - Initialize %wpa_supplicant
* @params: Parameters for %wpa_supplicant
* Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
*
* This function is used to initialize %wpa_supplicant. After successful
* initialization, the returned data pointer can be used to add and remove
* network interfaces, and eventually, to deinitialize %wpa_supplicant.
*/
這個函數(shù)先分配了struct wpa_global *global這個局部指向結(jié)構(gòu)體指針的內(nèi)存空間,然后通過傳遞的params參數(shù)填充global指向的結(jié)構(gòu)體中內(nèi)嵌的struct wpa_params對象,緊接著把global這個指針傳遞給static struct eloop_data eloop這個全局結(jié)構(gòu)體對象!
下面分別調(diào)用global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global); /* ctrl_iface_unix.c */
對于第一個接口的初始化,實際上通過socket進行了內(nèi)部進程間通信,如下
priv->sock = android_get_control_socket(global->params.ctrl_interface);
/* 此處通過getenv獲得了sockfd(android平臺),相當于如果本身有了fd的話,將直接跳轉(zhuǎn)到havesock,如果沒有的話,將創(chuàng)建連接,如下所示 */
priv->sock = socket(PF_UNIX, SOCK_DGRAM, 0); /* PF_UNIX 代表內(nèi)部進程間通信 */
下面bind或者connect,錯誤基本上也是goto到fail.到這里控制接口初始化結(jié)束.
下面初始化第二個接口dbus
global->dbus_ctrl_iface = wpa_supplicant_dbus_ctrl_iface_init(global); / *初始化dbus控制接口ctrl_iface_dbus.c */
再下面調(diào)用wpa_supplicant_daemon(global->params.pid_file)); 再調(diào)用os_daemonize函數(shù)寫pid(os_unix.c) ,最后返回局部變量global,到此初始化過程結(jié)束了!
4.3 wpa_supplicant_add_iface(global, &ifaces[i])
還是先看下這個函數(shù)注釋:
/**
* wpa_supplicant_add_iface - Add a new network interface
* @global: Pointer to global data from wpa_supplicant_init()
* @iface: Interface configuration options
* Returns: Pointer to the created interface or %NULL on failure
*
* This function is used to add new network interfaces for %wpa_supplicant.
* This can be called before wpa_supplicant_run() to add interfaces before the
* main event loop has been started. In addition, new interfaces can be added
* dynamically while %wpa_supplicant is already running. This could happen,
* e.g., when a hotplug network adapter is inserted.
*/
a.分配struct wpa_supplicant *wpa_s局部指針空間
b.init iface and iface2
wpa_supplicant_init_iface(wpa_s, iface) || wpa_supplicant_init_iface2(wpa_s, global->params.wait_for_interface)
這兩個函數(shù)很重要,將調(diào)用到驅(qū)動,下面我們分析下這兩個函數(shù)的調(diào)用過程
b1) wpa_supplicant_init_iface
b1.1)wpa_supplicant_set_driver(wpa_s, iface->driver)
此函數(shù)很重要,調(diào)用過程如下所示
wpa_s->driver = wpa_supplicant_drivers[i]; /* wpa_supplicant.c */
后者在drivers.c中 struct wpa_driver_ops *wpa_supplicant_drivers[ ] = { &wpa_driver_wext_ops, NULL };
ops這個結(jié)構(gòu)體對象注冊了一系列wext(我們采用wext的驅(qū)動類型)相關的函數(shù)指針,從而提供了相應的驅(qū)動接口!同時android增加了
#ifdef ANDROID
.driver_cmd = wpa_driver_priv_driver_cmd,
#endif
b1.2) 讀配置文件
讀取配置文件,并將其中的信息設置到wpa_supplicant數(shù)據(jù)結(jié)構(gòu)中的conf 指針指向的數(shù)據(jù)結(jié)構(gòu),它是一個wpa_config類型
命令行設置的控制接口ctrl_interface和驅(qū)動參數(shù)driver_param覆蓋配置文件里設置,命令行中的優(yōu)先;
對 于網(wǎng)絡配置塊有兩個鏈表描述它,一個是 config->ssid,它按照配置文件中的順序依次掛載在這個鏈表上,還有一個是pssid,它是一個二級指針,指向一個指針數(shù)組,該指針數(shù)組 按照優(yōu)先級從高到底的順序依次保存wpa_ssid指針,相同優(yōu)先級的在同一鏈表中掛載。
wpa_s->conf = wpa_config_read(wpa_s->confname); /* 此函數(shù)在wpa_supplicant.c中,而實現(xiàn)在conf_file.c中,這里內(nèi)容比較多,沒細看 */
b1.3) 將函數(shù)傳遞的指向結(jié)構(gòu)體wpa_interface的指針中的相關的ifname傳遞給指向wpa_supplicant結(jié)構(gòu)體的指針,調(diào)用結(jié)束.
b2) wpa_supplicant_init_iface2
調(diào)用wpa_supplicant_init_eapol()函數(shù)來初始化eapol;(eapol 網(wǎng)上查了下意思是:局域網(wǎng)擴展協(xié)議,請高手指點下這個是做什么用的?)
調(diào)用相應類型的driver的init()函數(shù),我們的是wext;(即.init = wpa_driver_wext_init,在driver_wext.c)
/* driver_ops 調(diào)用方式都和下面的類似 */
調(diào)用wpa_supplicant_driver_init()函數(shù),來初始化driver接口參數(shù),如下所示:
在該函數(shù)的最后會
wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
wpa_supplicant_req_scan(wpa_s, interface_count, 100000); 來主動發(fā)起scan,這個地方剛剛開始一直沒能理解清楚如何去scan,然后又是如何終止的,后來發(fā)現(xiàn)其實就是用了signal的方式來處理,下面是run scan的過程,代碼封裝的相當好,真正的執(zhí)行是在最后一個wpa_supplicant_run函數(shù)!
這樣這個wpa_supplicant_driver_init(wpa_s, wait_for_interface)函數(shù)就說完了!
下面再調(diào)用wpa_supplicant_ctrl_iface_init()函數(shù),來初始化控制接口;對于UNIX SOCKET這種方式,其本地socket文件是由配置文件里的 ctrl_interface參數(shù)指定的路徑加上網(wǎng)絡接口名稱;
這樣就講完了這個牛逼函數(shù),下面就是注冊dbus,不再詳解了,看代碼:
4.4 wpa_supplicant_run()函數(shù)
初始化完成之后,讓wpa_supplicant的main event loop run起來。
在 wpa_supplicant中,有許多與外界通信的socket,它們都是需要注冊到eloop event模塊中的,具體地說,就是在eloop_sock_table中增加一項記錄,其中包括了sock_fd, handle, eloop_data, user_data。
eloop event模塊就是將這些socket組織起來,統(tǒng)一管理,然后在eloop_run中利用select機制來管理socket的通信。
這個地方的真正執(zhí)行過程還沒理解清楚,只能以后有時間再理解了!
5 wpa_supplicant 的對外接口分析
對于wpa_supplicant模塊的對外接口,主要有以下幾種:
5.1. global control interface: 用于配置(增加或刪除)網(wǎng)絡接口。
5.2. ctrl interface: 與其他外部模塊交互的控制接口。
例 如,在初始化時,android 平臺的wifi.c中的 wifi_connect_to_supplicant函數(shù)調(diào)用wpa_ctrl_open函數(shù)創(chuàng)建兩個socket,一個是ctrl interface,另一個就是monitor interface,monitor interface這個接口用于監(jiān)測從wpa_supplicant發(fā)出的event事件。
這兩個socket創(chuàng)建成功后,monitor interface 會發(fā)送ATTACH到wpa_supplicant模塊,wpa_supplicant模塊收到后,會將該客戶端的socket信息記錄下來,用于以后發(fā)送事件時用(由于用的是DGRAM的方式)。
5.3. socket for ioctl: 發(fā)送命令到kernel space。
5.4. socket (netlink) for interact between kernel and userspace(AF_NETLINK, NETLINK_ROUTE): 接受kernel發(fā)送上來的event。
5.5. socket for l2 packet(PF_PACKET): 處理802.1x報文。
聯(lián)系客服