Linux Wireless之80211(nl80211, cfg80211, mac80211)

前言

在Linux无线子系统中,cfg80211mac80211nl80211是三个关键的组件,它们共同工作以实现对802.11设备的配置和管理。
cfg80211负责内核空间的通用API,mac80211负责具体的MAC层实现,而nl80211则作为用户空间与内核空间之间的桥梁,用于配置管理和通信。
80211.png

nl80211

nl80211是介于用户空间与内核空间之间的 API ,可以算是 cfg80211 的前端,也会生成 “事件” (events) 信息。该模块依赖 netlink 协议来在两个空间进行信息交互,通过socket接收上层命令,执行对应函数进行配置管理网络接口。Netlink 是一个 Linux 中的 socket 类型,用于在内核与用户空间之间传递事件。

nl80211 is the new 802.11 netlink interface public header. Together with cfg80211 it is intended to replace Wireless-Extensions. nl80211 and cfg80211 are still under development.

我们常用的一些无线网络工具组件都使用了nl80211:

cfg80211

cfg80211实现了wlan设备的注册,上可接收用户态配置管理命令,下可通过mac80211进行和硬件交互。因为实现了设备注册,虚拟接口的区分也是在此定义实现的(struct wiphy,一个 wiphy 设备可以有一个对应 MAC)。

cfg80211 is the Linux 802.11 configuration API. cfg80211 replaces Wireless-Extensions. nl80211 is used to configure a cfg80211 device and is used for kernel ←→ userspace communication.
cfg80211 is now feature-par complete with wireless-extensions, it actually has a lot more features that are simply not available and will never be available through wireless extensions. When implementing a cfg80211 driver wireless extensions support is still provided automatically for you through cfg80211 through CONFIG_CFG80211_WEXT. Distributions no longer needing wireless extensions can remove this and are encouraged to do so. cfg80211 also provides full regulatory support, this is done through wireless-regdb and the usage of CRDA.

All new Linux wireless drivers should be written targeting either cfg80211 for fullmac devices or mac80211 for softmac devices.

cfg80211 contains code to help enforce regulatory spectrum use restrictions.

mac80211

这是最底层的模块,与 hardware offloading 关联最多。如果某些功能无法由设备硬件实现,那么就可以以纯软的方式实现在这里。另外,以软件形式实现也可以赋予开发者对逻辑有更大的控制权。其也被称为 “Soft MAC” 模块,与 “Hard MAC” (由设备固件完成所有工作)相对。实际场景中,通常是这两种方案混合使用。802.11 协议状态机就在这里,需要处理所有类型的帧。

mac80211 is a framework which driver developers can use to write drivers for SoftMAC wireless devices.

SoftMAC devices allow for a finer control of the hardware, allowing for 802.11 frame management to be done in software for them, for both parsing and generation of 802.11 wireless frames. Most 802.11 devices today tend to be of this type.

mac80211 implements the cfg80211 callbacks for SoftMAC devices, mac80211 then depends on cfg80211 for both registration to the networking subsystem and for configuration. Configuration is handled by cfg80211 both through nl80211 and wireless extensions.

In mac80211 the MLME is done in the kernel for station mode (STA) and in userspace for AP mode (hostapd).

If you have new userspace utilities which support nl80211 you do not need wireless-extensions to support a mac80211 device.

mac80211支持下面一些特性:

  • IEEE 802.11abgn
  • IEEE 802.11d
  • Integration of work for the emerging 802.11s standard
  • Roaming using wpa_supplicant (802.11r as well). See Roaming TODO section for more details
  • Different types of interfaces, see supported wireless modes for details.
  • Vendor specific rate support
  • QoS
  • all mac80211 drivers get monitor mode support

参考