目錄
1,iw工具是什麼?
2,iw工作原理以及流程是什麼?
2.1,全局概覽
2.2,源碼分析
3,iw添加自定義指令
3.1,添加指令:iw dev wlan0 get test_ps
1,iw工具是什麼?
iw是linux系統上的一款無線配置工具,它的出現為了解決iwconfig的很多不足。之是以要更新開發一套無線配置工具,還要從無線拓展(Wireless-Extensions)說起。Wireless-Extensions(簡稱WE或者Wext)是有Jean Tourrilhes 1997年開發并添加到linux核心的,他通過linux的系統調用ioctl()來實作使用者層和核心層之間的通信。由于設計的比較粗糙,使用WE開發的程式難以管理,WE現在除了基本的bugfix之外也無人維護。于是出現了一種新的無線驅動架構來指導無線程式開發,便出現cfg80211和nl80211。cfg80211不再使用ioctl系統調用,而是使用Netlink。iw就是完全基于cfg80211架構重新設計并開發的。
2,iw工作原理以及流程是什麼?
2.1,全局概覽
我們可以看出,各個子產品之間的界限清晰,子產品之間透明不可見,子產品之間一般不會互相影響。本章重點關注是iw----(netlink)----cfg80211這部分的實作,其他子產品以後的章節來詳細說明。最終關注的還是層與層之間的收發函數。
2.2,源碼分析
//iw工作關鍵流程
int main(int argc, char **argv)
{
/*nl80211初始化,nl80211_init(&nlstate)*/
//給netlink socket配置設定空間
state->nl_sock = nl_socket_alloc();
//連接配接核心的Generic NetLink
genl_connect(state->nl_sock);
//擷取nl80211的驅動ID
state->nl80211_id = genl_ctrl_resolve(state->nl_sock, "nl80211");
/*解析使用者輸入指令*/
if (strcmp(*argv, "dev") == 0 && argc > 1) {
__handle_cmd(&nlstate, II_NETDEV, argc, argv, &cmd);
}
/*以下是__handle_cmd()函數實作*/
for_each_cmd(sectcmd) { //在__cmd的section中查找cmd的實作
if (strcmp(cmd->name, command)
match = cmd;
}
cmd = match; //cmd結構體指派(name, handler,...)
//申請mesg消息記憶體
msg = nlmsg_alloc();
//填充msg
genlmsg_put(msg, 0, 0, state->nl80211_id, 0,cmd->nl_msg_flags, cmd->cmd, 0);
//回調函數初始化,運作cmd定義的handler()函數
cmd->handler(state, msg, argc, argv, idby);
//配置回調
nl_socket_set_cb(state->nl_sock, s_cb);
//發送msg
nl_send_auto_complete(state->nl_sock, msg);
//設定事件的具體回調函數
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
//接收msg
while(err > 0) {
nl_recvmsgs(state->nl_sock, cb); //接收資料,回調函數
}
}
//指令實作(interface.c):iw dev wlan0 info
/*關鍵的結構體以及宏定義*/
struct cmd {
const char *name;
const char *args;
const char *help;
const enum nl80211_commands cmd;
int nl_msg_flags;
int hidden;
const enum command_identify_by idby;
/*
* The handler should return a negative error code,
* zero on success, 1 if the arguments were wrong.
* Return 2 iff you provide the error message yourself.
*/
int (*handler)(struct nl80211_state *state,
struct nl_msg *msg,
int argc, char **argv,
enum id_input id);
const struct cmd *(*selector)(int argc, char **argv);
const struct cmd *parent;
}
#define TOPLEVEL(_name, _args, _nlcmd, _flags, _idby, _handler, _help) \
struct cmd \
__section ## _ ## _name \
__attribute__((used)) __attribute__((section("__cmd"))) = { \
.name = (#_name), \
.args = (_args), \
.cmd = (_nlcmd), \
.nl_msg_flags = (_flags), \
.idby = (_idby), \
.handler = (_handler), \
.help = (_help), \
}
#define COMMAND(section, name, args, cmd, flags, idby, handler, help) \
__COMMAND(&(__section ## _ ## section), name, #name, args, cmd, flags, 0, idby, handler, help, NULL)
/*指令:iw dev wlan0 info分析*/
/*TOPLEVEL 來定義 scan操作*/
TOPLEVEL(info, NULL, NL80211_CMD_GET_INTERFACE, 0, CIB_NETDEV, handle_interface_info,
"Show information for this interface.");
/*
展開後:
cmd {
.name = info,
.args = NULL,
.cmd = NL80211_CMD_GET_INTERFACE,
.nl_msg_flags = 0,
.idby = CIB_NETDEV,
.handler = handle_interface_info,
.help = "Show information for this interface.",
};
*/
//cmd->handler()
static int handle_interface_info(struct nl80211_state *state, struct nl_msg *msg,
int argc, char **argv, enum id_input id)
{
register_handler(print_iface_handler, NULL); //注冊到回調函數--》valid_handler()
return 0;
}
//格式列印msg消息
static int print_iface_handler(struct nl_msg *msg, void *arg)
{
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
printf("%sInterface %s\n", indent, nla_get_string(tb_msg[NL80211_ATTR_IFNAME]));
printf("%s\tifindex %d\n", indent, nla_get_u32(tb_msg[NL80211_ATTR_IFINDEX]));
//......
return NL_SKIP;
}
3,iw添加自定義指令
3.1,添加指令:iw dev wlan0 get test_ps
3.1.1,新增test_ps.c
#include <errno.h>
#include <string.h>
#include <netlink/genl/genl.h>
#include <netlink/msg.h>
#include <netlink/attr.h>
#include "nl80211.h"
#include "iw.h"
static int print_test_power_save_handler(struct nl_msg *msg, void *arg)
{
struct nlattr *attrs[NL80211_ATTR_MAX + 1];
struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); //擷取netlink的payload
const char *s;
nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh,
0), NULL); //attrs save data
if (!attrs[NL80211_ATTR_PS_STATE]) {
return NL_SKIP;
}
switch (nla_get_u32(attrs[NL80211_ATTR_PS_STATE])) {
case NL80211_PS_ENABLED:
s = "test_on";
break;
case NL80211_PS_DISABLED:
default:
s = "test_off";
break;
}
printf("test power save: %s\n", s);
return NL_SKIP;
}
static int get_test_power_save(struct nl80211_state *state,
struct nl_msg *msg,
int argc, char **argv,
enum id_input id)
{
register_handler(print_test_power_save_handler, NULL);
return 0;
}
COMMAND(get, test_ps, "<param>",
NL80211_CMD_GET_POWER_SAVE, 0, CIB_NETDEV, get_test_power_save,
"Retrieve test power save state.");
3.1.2,修改Makefile
3.1.3,測試結果
root@:~# ./iw dev wlan0 get test_ps
test power save: test_off
四,總結
對iw工具源碼的分析,可以發現解耦性很強,具體的指令實作都是通過某個C檔案來實作(如掃描相關的scan.c,如接口相關的interface.c等),并使用了section的方式新增到使用者自定義字段中,調用前無需聲明,直接通過for_each_cmd()方式查找。接下來的文章将對cfg80211,mac80211相關知識整理,歡迎訂閱公衆号,一起學習交流!