本文主要是介绍wifi底层学习之路:一,iw工具开启无线底层大门钥匙,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
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 NetLinkgenl_connect(state->nl_sock);//获取nl80211的驱动IDstate->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();//填充msggenlmsg_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);//发送msgnl_send_auto_complete(state->nl_sock, msg); //设置事件的具体回调函数nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL); //接收msgwhile(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的payloadconst char *s;nla_parse(attrs, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),genlmsg_attrlen(gnlh, 0), NULL); //attrs save dataif (!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相关知识整理,欢迎订阅公众号,一起学习交流!
这篇关于wifi底层学习之路:一,iw工具开启无线底层大门钥匙的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!