hostapd阅读(openwrt)-2

  深入追踪openwrt下的hostapd之后,发现openwrt无线管理机制格外的复杂,几乎所以的触发与回调均离不开ubus,关于ubus这里不作解释,先大概了解其用途即可(出门左转:https://wiki.openwrt.org/doc/techref/ubus)。咱们先来了解openwrt下无线启动,我们首先查看系统启动脚本:/etc/preinit,发现该脚本主要是系统的检验与初始化,然后我们进入/etc/init.d/:

root@openwrt:/etc/init.d# ls
cron         done         firewall     log          odhcpd       rpcd         sysfixtime   system       uhttpd
boot         dropbear     led          network      protectx     sysctl       sysntpd      telnet       umount

通过查看与网络相关的启动脚本,最后发现network文件里面,出现:

start_service() {
        init_switch

        procd_open_instance
        procd_set_param command /sbin/netifd
        procd_set_param respawn
        procd_set_param watch network.interface
        [ -e /proc/sys/kernel/core_pattern ] && {
                procd_set_param limits core="unlimited"
                echo '/tmp/%e.%p.%s.%t.core' > /proc/sys/kernel/core_pattern
        }
        procd_close_instance
}

通过查阅相关资料发现netifd大有文章(https://wiki.openwrt.org/doc/techref/netifd), 简单的来说,netifd是老版本openwrt脚本初始化与配置网络的替代品,主要底层构建在netlink之上。netifd可以监视/etc/config/network配置文件,这样一来可能通过/etc/init.d/network reload重新加载网络,而不用每次重启全部的网络组件,netifd主要的组成有:

openwrt编译目录为:package/network/config/netifd
1. Shell脚本:
    /sbin/ifup,
    /sbin/ifdown(link to ifup), 
    /sbin/ifstatus, 
    /sbin/devstatus
2. init.d脚本:
    /etc/init.d/network
3. hotplug2脚本:
    /etc/hotplug.d/iface/00-netstate,
    /etc/hotplug.d/iface/10-sysctl
4. udhcpc脚本:
    /usr/share/udhcpc/default.script
5. netifd守护进程binary:
    /sbin/netifd
其它相关:
    hostapd(hostapd.sh)
    dhcp
    等等

netifd在启动时候会调用网络相关组件进行初始化:

void netifd_init_script_handlers(int dir_fd, script_dump_cb cb)
{
    glob_t g;
    int i, prev_fd;

    prev_fd = netifd_dir_push(dir_fd);
    glob("./*.sh", 0, NULL, &g);
    for (i = 0; i < g.gl_pathc; i++)
        netifd_parse_script_handler(g.gl_pathv[i], cb);
    netifd_dir_pop(prev_fd);
}

上述函数中即调用了hostapd的启动脚本:
lib/netifd/wireless/mac80211.sh 向ubus注册了网络接口处理函数
int netifd_ubus_init(const char *path) { uloop_init(); ubus_path = path; ubus_ctx = ubus_connect(path); if (!ubus_ctx) return -EIO; DPRINTF("connected as %08x ", ubus_ctx->local_id); ubus_ctx->connection_lost = netifd_ubus_connection_lost; netifd_ubus_add_fd(); netifd_add_object(&main_object); netifd_add_object(&dev_object); netifd_add_object(&wireless_object); netifd_add_iface_object(); return 0; }
原文地址:https://www.cnblogs.com/shizhai/p/5784365.html