查看: 1128|回复: 0

[技术交流] WIFI底层学习路程:iw,cfg80211,mac80211

[复制链接]

185

主题

204

帖子

596

积分

利尔达员工

Rank: 9Rank: 9Rank: 9

积分
596
发表于 2020-11-3 09:53:49 | 显示全部楼层 |阅读模式
  一,如何开启wifi底层学习呢?

  茫茫的内核源码怎么找到切入点呢,学习路线通常是从熟悉入门到陌生知识。因此iw工具是入门无线学习的钥匙。学习路线:iw-->cfg80211-->mac80211-->无线驱动(ath9k等),全局概述如下:

  二,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框架重新设计并开发的。

  三,源码分析

  3,1,iw工作关键流程

  //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);  //接收数据,回调函数
   }
  }

  3.2,指令实现(interface.c):iw dev wlan0 info

  //指令实现(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;
  }

  四,iw添加自定义指令

  4.1,添加指令:iw dev wlan0 get test_ps

  4.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.");

  4.1.2,修改Makefile

  4.1.3,测试结果

  root@:~# ./iw dev wlan0 get test_ps
  test power save: test_off

  五,总结

  经过对iw工具源码的分析,可以发现解耦性很强,具体的指令实现都是通过某个C文件来实现(如扫描相关的scan.c,如接口相关的interface.c等),并使用了section的方式新增到用户自定义字段中,调用前无需声明,直接通过for_each_cmd()方式查找。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表