1. 程式人生 > >Linux, Netlink, and Go — Part 2: generic netlink

Linux, Netlink, and Go — Part 2: generic netlink

Linux, Netlink, and Go — Part 2: generic netlink

In Part 1 of this series, I described some of the fundamental concepts of netlink sockets, messages, and attributes. It is assumed that readers are already familiar with netlink from the previous post in this series.

In this post, I will dive into generic netlink

, a specialized netlink family designed to be more extensible than a typical netlink family.

The pseudo-code in this series will use Go’s x/sys/unix package and types from my netlink and wifi packages. The series is split into parts as follows:

What is generic netlink?

Generic netlink was designed to allow kernel modules to easily communicate with userspace applications using netlink. Instead of creating a new top level netlink family for each module, generic netlink provides a simplified interface to enable modules to plug into the generic netlink bus.

Generic netlink messages

Generic netlink makes use of a message with a small payload. This message occupies the first four bytes of a netlink message payload, and looks like so:

  • Command (8 bits): specifies which command to issue to a generic netlink family.
  • Version (8 bits): the version of a command to issue to generic netlink.

2 bytes of padding immediately follow these fields. Remember, netlink makes use of 4 byte boundaries! After the padding, a generic netlink message payload is present. This payload may contain data (such as netlink attributes) as parameters for a command, or data in response to a command.

Generic netlink families

Not to be confused with netlink’s families, generic netlink also has a concept of families: kernel modules which register with the generic netlink bus for communication. Some examples of these may include:

  • nlctrl: the generic netlink controller, used to determine which generic netlink families are available. Present on all systems where generic netlink is available.
  • TASKSTATS: provides per-task and per-process statistics from the kernel to userspace.
  • nl80211: provides access to IEEE 802.11 WiFi device statistics and interactions.

nlctrl: the generic netlink controller

In order to discover which generic netlink families are available on a given machine, a request can be sent to the generic netlink controller. The controller is a special family which is present on all machines where generic netlink is available.

To send a message to the controller, its family ID (always 0x10) is used in the Type field of the outer netlink header. The payload of the netlink message contains the generic netlink header, which specifies a command and its version. Finally, parameters can be passed as netlink attributes in the body of the generic netlink message.

When you put it all together, the command “controller: list all generic netlink families” looks something like this:

msg := netlink.Message{
Header: netlink.Header{
// Specify nlctrl's type (0x10) to communicate with it.
Type: genetlink.Controller,
Flags: netlink.HeaderFlagsRequest | netlink.HeaderFlagsDump,
// Some fields omitted for brevity.
},
// The generic netlink header and data are wrapped in a
// netlink message, marshaled into byte form.
Data: marshal(genetlink.Message{
Header: genetlink.Header{
Command: ctrlCommandGetFamily,
Version: ctrlVersion,
},
}),
}

Requesting information about a single family from the controller is slightly different. The “dump” flag is omitted from the netlink header, and the generic netlink message payload contains attributes which specify the name of a specific family:

// Used in Data field of genetlink.Message.
b := netlink.MarshalAttributes([]netlink.Attribute{{
Type: attrFamilyName,
// Null-terminated string in byte form.
Data: nlenc.Bytes("nl80211"),
}})

Many of these constants are taken from the generic netlink kernel headers. To see them in action in Go source code, you may also reference package genetlink.

Generic netlink family attributes

Family information is returned in a generic netlink message payload as a series of netlink attributes. These attributes include information about a generic netlink family, such as:

  • ID (16 bits): unique identifier for family. Note: ID may change between reboots or if certain kernel modules are loaded or unloaded. Always perform a lookup by name to retrieve a family’s ID!
  • Name (null-terminated string): human-readable name for the family, like “nlctrl”, “TASKSTATS”, or “nl80211”.
  • Version (32 bits): version of generic netlink family. Oddly, this field is 32 bits while the version field in the generic netlink header is 8 bits. I have never seen this value occupy more than 8 bits.
  • Multicast groups (nested attributes): netlink attribute “array” with attribute type incremented by one for each element. Contains additional nested attributes with multicast group name (null-terminated string) and ID (32 bits).

Several other attributes exist as well, but as I have not worked with them, I cannot explain their use.

Summary

Thanks to the foundation provided by netlink, generic netlink provides a simple and elegant mechanism for extending netlink. There are many available generic netlink families, and the list may differ greatly from system to system.

To discover which generic netlink families are available on your machine, try genl-ctrl-list. Specifying -d provides a great amount of detail about each family.

$ genl-ctrl-list
0x0010 nlctrl version 2
0x0011 VFS_DQUOT version 1
0x0013 NLBL_MGMT version 3
0x0014 NLBL_CIPSOv4 version 3
0x0015 NLBL_UNLBL version 3
0x0016 acpi_event version 1
0x0017 thermal_event version 1
0x0018 tcp_metrics version 1
0x0019 TASKSTATS version 1
0x001a nl80211 version 1

The final part of this series will detail high-level usage of netlink, generic netlink, and nl80211, using Go packages netlink and wifi.

Thanks again for reading! If you have questions or comments, feel free to reach out via the comments, Twitter, or Gophers Slack (username: mdlayher).

References

  • A lot of time spent running iw commands with the NLCB=debug environment variable. nlmon virtual interfaces are also quite useful for capturing netlink traffic.

相關推薦

Linux, Netlink, and Go — Part 2: generic netlink

Linux, Netlink, and Go — Part 2: generic netlinkIn Part 1 of this series, I described some of the fundamental concepts of netlink sockets, messages, and at

Linux, Netlink, and Go — Part 1: netlink

Linux, Netlink, and Go — Part 1: netlinkI’m a big fan of Prometheus. I use it quite a lot at both home and work, and greatly enjoy having insight into what

Namespaces and Go Part 2

In Part 1 of Namespace article series ,we were unable to set hostname from a shell even though the user was root That program was missing UTS namespace w

Stanford Algorithms Design and Analysis Part 2 week 1

import java.io.BufferedReader;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException

Coursera course review: Algorithms: Design and Analysis, Part 2

I recently finished the Coursera course Algorithms: Design and Analysis, Part 2 by Professor Tim Roughgarden of Stanford. I’ve already reviewed part 1

Namespaces and Go Part 1

Linux provides the following namespaces and we will see how we can demonstrate these with Go Namespace Constant Isolates Cgroup

Namespaces and Go Part 3

In Part 2 we executed a shell with modified hostname using UTS namespace. In this article, we will explain how we can use PID and Mount namespaces. By is

[Stanford Algorithms: Design and Analysis, Part 2] c28 Sequence Alignment Optimal Substructure

sub ctu ali sta hms bubuko width height bsp [Stanford Algorithms: Design and Analysis, Part 2] c28 Seque

Codeforces Round #442 (Div. 2) 877E - Danil and a Part-time Job dfs序+線段樹

pac upd style init problem def ios const clas Codeforces Round #442 (Div. 2) 877E - Danil and a Part-time Job emmmm第一次見的東西感覺都好神奇 #inclu

C++ and OO Num. Comp. Sci. Eng. - Part 2.

its ted 增加 數字 gin logs markdown 模板函數 mar 本文參考自《C++ and Object-Oriented Numeric Computing for Scientists and Engineers》。 1. Basic Types 在

Generic Netlink核心實現分析(二):通訊

前一篇博文中分析了Generic Netlink的訊息結構及核心初始化流程,本文中通過一個示例程式來了解Generic Netlink在核心和應用層之間的單播通訊流程。 示例程式:demo_genetlink_kern.c(核心模組)、demo_genetlink_

Create a personal video watchlist in the cloud with PHP and the Movie Database API Part 2

If you have been following along with Part 1, you are half-way through building a web-based PHP application to store your personal wa

Data Legibility and a Common Language: Coping Not Coding, part 2

Why Data Legibility is More Important than Explainability< Pt 1: Social Infrastructure | Pt 2: New Public Amenities (coming soon)>Data needs to be le

Marginally Interesting: Command Line Interactive Machine Learning on the JVM. Part 2: JRuby and Scala

Tweet This is Part 2 of a series. Previous post is here. In order to b

Design Systems and Agility (Part 1 of 2)

Our working environmentsIn modern working contexts, our awareness of change still can freeze teams and can cause delays in decision making. This is because

半閒居士視覺SLAM十四講筆記(2)初識 SLAM- part 2 linux CMake、Kdevelop

該講詳細資料下載連結 【Baidu Yun】【Video】【Code】 若您覺得本博文對您有幫助,請支援高博的新書《視覺SLAM十四講》,【點選購買】 若您覺得本博文對您有幫助,請支援高

linux c檢測網線熱插拔(netlink)

#include <sys/types.h> #include <sys/socket.h> #include <asm/types.h> #include <linux/netlink.h> #include

Go 構建一個區塊鏈 -- Part 2: 工作量證明

翻譯的系列文章我已經放到了 GitHub 上:blockchain-tutorial,後續如有更新都會在 GitHub 上,可能就不在這裡同步了。如果想直接執行程式碼,也可以 clone GitHub 上的教程倉庫,進入 src 目錄執行 make 即可。

四、無線資訊傳遞——Generic netlink(二、通訊)

系列說明   仍舊是系列二: 1、無線驅動資訊傳遞框架:說明無線資訊傳遞的步驟流程以及各程式塊之間的聯絡; 2、generic Netlink訊號傳遞機制:hostapd與無線驅動之間的資訊傳遞機制; 3、以ssid為例說明使用者將user space中的

三、無線資訊傳遞——Generic Netlink(一、初始化)

系列說明   上一篇說明了無線資訊的user space至kernel space的大致傳遞流程,這一主要針對以下3點進行一個順序的描述: 1、無線驅動資訊傳遞框架:說明無線資訊傳遞的步驟流程以及各程式塊之間的聯絡; 2、generic Netlink訊號